Friday, February 12, 2010

In praise of ELMAH

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/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/elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />

Update: I was contacted by Phil Haselden shortly after I wrote this post, pointing me to this stack overflow answer:
http://stackoverflow.com/questions/1245364/securing-elmah-in-asp-net-website
You need to secure the path to ‘elmah.axd’ by making it a child of a ‘virtual’ directory, which in my case I’ve named ‘elmah’, and then only allowing admin users access to that directory. If you don’t do this and simply secure ‘elmah.axd’, it’s possible for a user to view your ELMAH page with a URL like http://sutekishop.co.uk/xxx/elmah.axd. I’ve updated this post now, so if you follow these instructions you should be fine.

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:

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

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">
  <system.web>
    <authorization>
      <allow users="the_admin_user"/>
      <deny users="*" />
    </authorization>
  </system.web>
</location>

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:

elmah

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:

elmahTweet

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.

2 comments:

  1. In praise of ELMAH >> I second you on that!!

    ReplyDelete
  2. Anonymous9:05 pm

    CodeSmith Tools recently released a new product, CodeSmith Insight, that is an excellent tool for error logging in .NET. Insight's application integration allows you to (among other things) report all unhandled exceptions just by referencing the Insight client assembly. Also, possibly best of all, it's free to get started!

    Check it out at http://www.codesmithinsight.com/

    ReplyDelete

Note: only a member of this blog may post a comment.