Over a million developers have joined DZone.

In Praise of ELMAH

DZone's Guide to

In Praise of ELMAH

· ·
Free Resource

Do you have an ASP.NET application? Do you want to record any errors it might throw? Do you want an easy way to view the errors and possibly get them emailed, or even tweeted to you? Look no further ….

ELMAH stands for ‘Error Logging Modules and Handlers’. It’s an incredibly easy to use but full featured set of error logging bolt-on bits for ASP.NET. The committers list on the project page reads like a who’s who of .NET, so you can’t argue that it’s not well supported. The documentation is pretty good so I’m not going to try and give a blow-by-blow account of how to set it up, I just want to show how I’ve used it in Suteki Shop and how easy and simple it is to get up and running.

The first thing to note about ELMAH is that it requires zero change to your application’s code base. The only thing you might want to do is remove any existing error loggin. In fact the best application to use ELMAH with, is one where errors are simply allowed to bubble up to the ‘yellow screen of death’. Don’t forget: The first rule of exception handling: do not do exception handling.

In Suteki Shop I’d previously used the MVC Framework’s default Rescue behaviour, so the only real code change I made was to remove the [Rescue("Default")] attribute from my ControllerBase class. I also had to add the ELMAH assembly to my dependencies folder. But most of the work consists of changes to Web.config.

First I added a new config section, then the ELMAH UI endpoint httpHandler that shows the error log. This is so I can type http://sutekishop.co.uk/elmah.axd and view any errors that Suteki Shop may throw (no it won’t work for you):

<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />

Next I added the ELMAH modules I required. I wanted to receive error notifications as tweets so I needed the core error log and the ErrorTweetModule:

<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
<add name="ErrorTweet" type="Elmah.ErrorTweetModule, Elmah"/>

The actual ELMAH configuration itself is done in the elmah config secion:

<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />
<security allowRemoteAccess="1" />
<errorTweet userName="sutekishop" password="sutekishops_password"
statusFormat="d @mikehadlow {Message}" />

You have to specify where you want the log to be stored - you can choose to put the log in a database, but I’m happy to have it written to an xml file in App_Data. I’ve also configured that I want to allow remote access so that I can view the log remotely.

To configure the twitter client I simply created a twitter account named ‘sutekishop’  and specified its user name, password and the format of the error message I want to send. Here I’m sending myself DMs of any error.

Lastly I configured the standard ASP.NET security so that only the admin account on suteki shop is able to view the elmah.axd page:

<location path="elmah.axd">
<allow users="the_admin_user"/>
<deny users="*" />

This means that if you try browsing to http://sutekishop.co.uk/elmah.axd you’ll be thrown back to the login page. If you could login as the administrator you would see this page:


Hmm, looks like a robot’s been doing some fishing and a user who’s session has expired is trying to go back and view their order. I probably shouldn’t throw for that, but return a nice information page instead.

The great thing is that I also get all these errors as tweets as well:


ELMAH is just so easy to configure and so easy to use that it seems the obvious first choice for error logging in any ASP.NET application.



Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}