ASP.NET MVC HandleError Attribute

Last Wednesday, I took a whopping 15 minutes out of my day and added ELMAH (Error Logging Modules and Handlers) to my ASP.NET MVC application.  If you haven’t heard the news (I hadn’t until recently), ELMAH does a killer job of logging and reporting nearly all unhandled exceptions.  As for handled exceptions, I’ve been using NLog but since I was already playing with the ELMAH bits I thought I’d see if I couldn’t replace it.

Atif Aziz provided a quick solution in his answer to a Stack Overflow question.  I’ll let you consult his answer to see how one can subclass the HandleErrorAttribute and override the OnException method in order to get the bits working.  I pretty much took rolled the recommended logic into my application and it worked like a charm. 

Along the way, I did uncover a few HandleError fact to which I wasn’t already privy.  Most of my learning came from Steven Sanderson’s book, Pro ASP.NET MVC Framework.  I’ve flipped through a bunch of the book and spent time on specific sections.  It’s a really good read if you’re looking to pick up an ASP.NET MVC reference.

Anyway, my notes are found a comments in the following code snippet.  I hope my notes clarify a few things for you too.

  1. public class LogAndHandleErrorAttribute : HandleErrorAttribute
  2. {
  3.     public override void OnException(ExceptionContext context)
  4.     {
  5.         // A word from our sponsors:
  6.         //
  7.         //      and Pro ASP.NET MVC Framework by Steven Sanderson
  8.         //
  9.         // Invoke the base implementation first. This should mark context.ExceptionHandled = true
  10.         // which stops ASP.NET from producing a "yellow screen of death." This also sets the
  11.         // Http StatusCode to 500 (internal server error.)
  12.         //
  13.         // Assuming Custom Errors aren't off, the base implementation will trigger the application
  14.         // to ultimately render the "Error" view from one of the following locations:
  15.         //
  16.         //      1. ~/Views/Controller/Error.aspx
  17.         //      2. ~/Views/Controller/Error.ascx
  18.         //      3. ~/Views/Shared/Error.aspx
  19.         //      4. ~/Views/Shared/Error.ascx
  20.         //
  21.         // "Error" is the default view, however, a specific view may be provided as an Attribute property.
  22.         // A notable point is the Custom Errors defaultRedirect is not considered in the redirection plan.
  23.         base.OnException(context);
  25.         var e = context.Exception;
  27.         // If the exception is unhandled, simply return and let Elmah handle the unhandled exception.
  28.         // Otherwise, try to use error signaling which involves the fully configured pipeline like logging,
  29.         // mailing, filtering and what have you). Failing that, see if the error should be filtered.
  30.         // If not, the error simply logged the exception.
  31.         if (!context.ExceptionHandled   
  32.             || RaiseErrorSignal(e)      
  33.             || IsFiltered(context))     
  34.             return;
  36.         LogException(e); // FYI. Simple Elmah logging doesn't handle mail notifications.
  37.     }