September 06, 2009

Tickle Me ELMAH

I came across a utility the other day that, in my opinion, just Gets It Right™. It’s easy to install, easy to configure, easy to use, and it’s got just the features you need, nothing more. It gets out of your way, and unobtrusively does its work. It’s free and open source. What more can you ask for?

The utility is called ELMAH. It’s an exception logging tool, and it’s unfortunate name is an acronym for “Error Logging Modules And Handlers”. Its Google Code site at describes itself as:
ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.
Once ELMAH has been dropped into a running web application and configured appropriately, you get the following facilities without changing a single line of your code:
  • Logging of nearly all unhandled exceptions.
  • A web page to remotely view the entire log of recoded exceptions.
  • A web page to remotely view the full details of any one logged exception.
  • In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with customErrors mode turned off.
  • An e-mail notification of each error at the time it occurs.
  • An RSS feed of the last 15 errors from the log.
I remember first reading about ELMAH a few months ago on Scott Hanselman’s blog, which led me to a Phil Haack blog post and then to Jeff Atwood’s Coding Horror. All of them have nothing but good things to say about ELMAH, and how easy it is to get up and running.

A couple of weeks ago I decided to get off my rump and see what all of the hoopla was about. Since I’ve been refactoring the BlogEngine.NET engine that runs this site, I’ve often been frustrated with the lack of error logging (and unit tests, but that’s another post). I’ve used log4net before, but I have never liked the effort it takes to integrate it into an existing codebase. ELMAH, however, seems to be a perfect fit with the “easy to install, easy to use” attitude of BlogEngine.

The ELMAH help pages along with Scott Hanselman’s and Phil Haack’s posts above give a lot of step-by-step installation instructions, so I’m not going to rehash them here. But, here’s a quick overview of what to expect if you want a basic, fully functional ELMAH installation:
  1. Download the ELMAH binaries
  2. Copy the ELMAH dll into your bin directory
  3. Add a few entries to your web.config file
  4. That’s it. There is no step 4.
Since I’m still using the out-of-the-box XML provider for BlogEngine, I simply added an “errors” folder in the App_Data directory. (I guess that can be your step 4.) I pointed my ELMAH configuration to save the XML log files in that directory, and everything just works.

I secured the log viewer page as described in Phil Haack’s post, and I then edited the web.sitemap file to add a new admin link pointing to it. That way, when I’m logged in, I can go straight to the errors page with a single click.

Less than 10 minutes later, and I have a pretty good, basic level of exception logging for BlogEngine, without having to add a single line of code. But, for a little context, be sure to read Atif Aziz’s response to Phil Haack’s post. Atif is the developer of ELMAH, and among other things, he says:
It's definitely not a general-purpose logging framework nor one for instrumentation. It only logs unhandled exceptions in a non-invasive way (speaking code-wise). I usually discourage people from using it to log errors in their catch blocks because that purpose is served better by existing tracing facilities and frameworks and often by actually handling the offending case gracefully in code.
Even so, I think it’s a great tool to have in your ASP.NET toolbox.