Old Website, Testing

Filtering Errors in ELMAH

August 28, 2011

There are very few projects I enjoy more than ELMAH. If you’ve never heard of it, please check out the project site for more details. It is essentially an open source gift from the software gods that lets lazy people like me easily do application-wide error logging. I use it on this blog and have no complaints. However as I intended, I will receive emails for every exception encountered. Even on a low-traffic site such as this, I end up with an absurd amount of 404 errors. So it would be nice if I could ignore the 404 errors and continue to be notified of the others.


 

ELMAH Can Do That!

Sweet! A quick search reveals that not only does ELMAH support filtering, but it is very easy to do like everything else associated with it. In fact, there are two ways of accomplishing this, programmatically and by configuration. I avoid Web.config whenever I can, so I’ll opt for the programmatic approach. This requires me to add a small bit of code to my Global.asax.cs file:

You’ll need to reference the Elmahnamespace, but it really is as simple as that. I have custom error pages so users still get redirected to my 404 error page, but without lighting up my phone with new emails.

Let’s Change Up The Requirements

So I might not have told you everything before. (I always wanted to act like a client!). The problem with handling all the 404 errors is that a good 99% of them are requests to images that no longer exist. Back in the day I’d use my site to host images on various message boards and when I implemented ELMAH I began to get an annoying amount of 404 errors for 50 variations of images like “fail.jpg”. So what I’d really like, is to filter out the common image-related 404 errors but still be notified of the others.

Thankfully that is simple to do as well. We only need to modify the Filter404Errors method that we added such as:

Obviously we could make that a lot more elegant. It certainly won’t catch all images, but it will catch all the ones I’m concerned about. So we’re good to go now, right?

I Was wrong, You Were Right…

So it turns out that even when I filter out 99% of the 404 errors, that it still breaks the threshold where I get annoyed enough to no longer care about them. So I’m going to filter out all of them and hope that if a legit 404 ever pops up that either I’ll notice it or someone will be kind enough to inform me about it. Regardless, filtering specific errors in ELMAH is as simple as it gets and still gives you the ability to be selective if you choose to be. I hope this can be helpful to anyone looking to filter certain errors. If not, please check out the official documentation on the ELMAH project page.

You Might Also Like

Leave a Reply

avatar