Setup email alerts from ELMAH when exceptions are raised
June 4, 2010 | by Adam Pope
This is part of a series of posts on using ELMAH to handle error logging for your ASP.NET web applications. We’ve already looked at how to get started with ELMAH and how to allow remote access to your error logs. In this post we are going to look at how to configure ELMAH to send you an email alert every time an exception occurs on your site. This is a really handy feature if you want to catch bugs early and keep your site running smoothly.
ELMAH makes sending email alerts really quite simple. Open up your web.config file and add an
errorMail section definition and corresponding
errorMail entry as follows:
<configSections> <sectionGroup name="elmah"> <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/> <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/> </sectionGroup> </configSections> <elmah> <errorLog type="Elmah.SQLiteErrorLog, Elmah" connectionStringName="..."/> <errorMail from="firstname.lastname@example.org" to="email@example.com"/> </elmah>
You’ll also need to add a reference to the
Elmah.ErrorMailModule in your
<system.web> <httpModules> <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/> <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/> </httpModules> </system.web>
You will then need to configure an SMTP server for firstname.lastname@example.org in your web.config. You can do this in two ways: inline on the
errorMail definition, or in a system.net
mailSettings section. Examples of both are shown below. Personally, I prefer to use a
mailSettings section as I use the SMTP details elsewhere in my applications.
<system.net> <mailSettings> <smtp deliveryMethod="network"> <network host="..." port="25" userName="..." password="..." /> </smtp> </mailSettings> </system.net> or <errorMail from="..." to="..." async="true" smtpServer="..." smtpPort="25" userName="..." password="..." />
If you want to send your error emails using GMail’s SMTP server you will have to change a couple of bits in your configuration. GMail requires SSL to be enabled. We cannot specify this through the system.net section so we need to add the useSsl parameter to the errorMail tag. We also need to specify port 587. If you don’t specify a port on your errorMail tag, ELMAH will use port 25 by defualt. To tell it to use the port we have defined in the system.net section, set
smtpPort="0". [Thanks to Scott Mitchell for this tip]
<elmah> <errorMail from="..." to="..." async="true" smtpPort="0" useSsl="true" /> </elmah> <system.net> <mailSettings> <smtp deliveryMethod="network"> <network host="smtp.gmail.com" port="587" userName="..." password="..." /> </stmp> </mailSettings> </system.net>
That’s all you need. I suggest creating a page with a deliberate error on to check that everything is set up correctly.
What Next? Well, if you have a team of developers working on a site then you probably have bug tracking software keeping track of problems that need working on. Most popular bug tracking packages allow you to post issues by email – why not pipe your error report emails straight into your bug tracker, after all problems that are happening now should be top of your todo list (See Jeff Atwood’s Exception-Driven Development).
Caution: If you experience email issues (maybe a password changes, or an SMTP relay is misconfigured) then you will not receive emails alerting you to the problem (or any other problems). This is a catch-22 that has caught me out a few times. If this becomes a problem for you, ELMAH also provides an RSS feed of errors which you could subscribe to, it can also Tweet your errors to you!