Sending Error Mail from Elmah

As part of a recent release of a web application we decided that in order to make the best use of error logs being created by Elmah we should email these to stakeholders as and when they happen to improve the general response times. Here is how we set up ‘that thing’ in our .NET MVC4 application! The red sections indicate properties that were either added or edited to enable the email functionality and green section indicate those fields you will need to change to reflect your choice of smtp server.



<?xml version="1.0" encoding="utf-8"?>
 <configuration>
    <configSections>
        ..........
        <sectionGroup name="elmah">
            <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
            <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
            <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
            <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
        </sectionGroup>
        .....
    </configSections>
    <connectionStrings>
        ......
    </connectionStrings>
    <appSettings>
        ......
    </appSettings>
    <system.web>
        .....
        <sessionState timeout="20" mode="InProc" customProvider="DefaultSessionProvider">
            ......
        </sessionState>
        <httpModules>
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
            <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
            <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
        </httpModules>
    </system.web>
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <modules>
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
            <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
            <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
        </modules>
    </system.webServer>
    <runtime>
        ...
    </runtime>
    <entityFramework>
        ...
    </entityFramework>
    <elmah>
        <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />
        <errorMail from="fromaddress@somewhere.com" to="toaddress@somewhere.com" async="true" smtpPort="0" useSsl="true" />
        <security allowRemoteAccess="false" />
    </elmah>
    <system.net> 
        <mailSettings> 
            <smtp deliveryMethod ="Network"> 
            <network host="smtp.gmail.com" port="587" userName="gmailuserName"   password="gmailPassword" /> 
          </smtp> 
        </mailSettings> 
    </system.net>
    <location path="elmah.axd" inheritInChildApplications="false">
        ...
    </location>
    <applicationSettings>
        ...
    </applicationSettings>
 </configuration>

Leave a Reply

Your email address will not be published. Required fields are marked *