Customizing ELMAH’s Error Emails

Published 06 January 11 10:40 PM | Scott Mitchell

ELMAH (Error Logging Modules and Handlers) is my ASP.NET logging facility of choice. It can be added to a new or running ASP.NET site in less than a minute. It’s open source and it’s creator, Atif Aziz, remains actively involved with the project and can be found answering questions about ELMAH, from Stackoverflow to ELMAH’s Google Discussion group. What’s not to love about it?

ELMAH’s sole purpose is to log and notify developers of errors that occur in an ASP.NET application. Error details can be logged to any number of log sources – SQL Server, MySQL, XML, Oracle, and so forth. Likewise, when an error occurs ELMAH can notify developers by sending the error details to one or more email addresses.

The notification email message sent by ELMAH is pretty straightforward: it contains the exception type and message, the date and time the exception was generated, the stack trace, a table of all server variables, and the Yellow Screen of Death that was generated by the error.


Prior to sending the notification email, ELMAH’s ErrorMailModule class raises its Mailing event. If you create an event handler for this event you can inspect details about the error that just occurred and modify the email message that is about to be sent. In this way you can customize the notification email, perhaps setting the priority based on the error or cc’ing certain email addresses if the error has originated from a particular page on the website.

To create an event handler for the Mailing event, open (or create) the Global.asax file and add the following syntax:

void ErrorMailModuleName_Mailing(object sender, Elmah.ErrorMailEventArgs e)

In the above code snippet, replace ErrorMailModuleName with the name you assigned the ErrorMailModule HTTP Module. This module may be defined in one or two places: the <system.web>/<httpModule> section and/or the <system.webServer>/<modules> section. The following Web.config snippet shows both sections:

        <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />


        <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />

The name for the module in both sections should be the same - in the above snippet the name is ErrorMail. Consequently, to create an event handler for ELMAH’s Mailing event with the above configuration we would use the syntax:

void ErrorMail_Mailing(object sender, Elmah.ErrorMailEventArgs e)

Note that the Mailing event handler’s second input parameter is of type ErrorMailEventArgs. This class provides two helpful properties:

  • Error – the error that was just logged by ELMAH. This property is of type Elmah.Error and has properties like Exception, Message, User, Time, and so on.
  • Mail – the MailMessage object that is about to be sent. This gives you an opportunity to modify the outgoing email.

The following Mailing event handler shows how you could adjust the notification email based on the type of exception that occurred. Here, if the error that just occurred was an ApplicationException then the notification email is set to a High priority, it’s subject it changed to “This is a high priority item!”, and is cc’d.

void ErrorMail_Mailing(object sender, Elmah.ErrorMailEventArgs e)
    if (e.Error.Exception is ApplicationException ||
        (e.Error.Exception is HttpUnhandledException && 
            e.Error.Exception.InnerException != null &&
            e.Error.Exception.InnerException is ApplicationException))
        e.Mail.Priority = System.Net.Mail.MailPriority.High;
        e.Mail.Subject = "This is a high priority item!";

Note that if an unhandled ApplicationException is what prompted ELMAH to record the error then by this point the original exception will have been wrapped in an HttpUnhandledException. So in the if statement above I check to see if the error’s Exception property is an ApplicationException or if it is an HttpUnhandledException exception with an InnerException that is an ApplicationException. If either of those conditions hold then I want to customize the notification email.

Happy Programming!

Filed under:


# Atif Aziz said on January 6, 2011 03:40 PM:

Hey Scott, thank for the write up! Just a small correction about the ErrorMailEventArgs.Error property. You said it represents the error that was just logged by ELMAH. In fact, it's just an Error representation of the unhandled or signaled Exception. It doesn't mean it has been logged because you could very well have a setup in which you just mail errors and never log them! That is, you could have the ErrorMailModule registered but not ErrorLogModule. Moreover, the error filtering could be setup such that you may want some errors mailed but not logged! Even if you have both logging and mailing turned on, it could be that logging failed due to database connectivity issue but the error could still be mailed if the SMTP infrastructure is up! In all these scenarios, the logging and mailing working independent of each other and by design.

If one does want a crack at the logged Error during mailing, one would have to pick it up in the ErrorLogModule.Logged event. This could be handy, for example, to push the Id of the logged error into the mail. To do that, you'd stash away the Id from the Logged event into the HttpContext and use it later in the Mailing event. For this to work, the modules would have be registered such that the Mailing event occurs after the Logged event.

# Roland said on January 7, 2011 04:23 AM:

Thanks for sharing. I have never used it and I should.

I am wondering if your tutorial about ELMAH is still worths reading?

# Scott Mitchell said on January 10, 2011 02:10 PM:

Roland, this is the de facto ELMAH tutorial:

I believe it is still relevant.

Leave a Comment



My Books

  • Teach Yourself ASP.NET 4 in 24 Hours
  • Teach Yourself ASP.NET 3.5 in 24 Hours
  • Teach Yourself ASP.NET 2.0 in 24 Hours
  • ASP.NET Data Web Controls Kick Start
  • ASP.NET: Tips, Tutorials, and Code
  • Designing Active Server Pages
  • Teach Yourself Active Server Pages 3.0 in 21 Days

I am a Microsoft MVP for ASP.NET.

I am an ASPInsider.