Saturday, 13 August 2011

ELMAH

In my last post about ASP.NET MVC I mentioned ELMAH (Error Logging Modules and Handlers) and thought I would expand on what it is and what it does. In essence it is a way of logging unhandled errors in your code - one of the key features which I like is it is able to be used on a running app without the need to recompile and deploy.

As a good programmer I am sure that you always try to catch errors and recover gracefully from them - you also probably log those errors to a database or file. Unfortunately however hard you try a user will find a way to break your well crafted code - of course if you have done things correctly they will see a nice custom error page, rather than the yellow screen of death.

The problem with this is that because these errors are unhandled and not logged finding out exactly what caused the error is not exactly easy, in my experience relying on users to give you any useful information is not a great strategy, in a perfect world what you really want is to be able to see the actual yellow screen of death with the full error and stack trace. This is what ELMAH gives you, with just a few lines in the Web.config it can log the error details to a database (or any other of its supported storage methods) - the error log can then be opened in a browser and in most cases the original error screen can be viewed.

The advantages for tracking individual errors are obviously massive however for every error that is reported there must be a whole bunch which are not reported - ELMAH lets you track these as well, in fact this is one of the main reasons the guys at Stackoverflow use it.

One small issue I would note is that the original code only works with CustomErrors set to false, which for live code is a big no-no, so is great for development purposes but it not really usable for a deployed app. To get around this limitation you can implement this patch by the original author.

It is easy to install (even on existing apps), gives you time saving information and in general makes your life easier - all this and it is free! So, if you are not using it, why not?

No comments:

Post a Comment