ASP.NET MVC – Exception Handling – Un Handled exception

In one of our new development, client team decided to start with ASP.NET MVC. As I knew only the plain MVC, I started exploring ASP.NET MVC little better. Over there, I tend to check about exception handling.  And found great about the in-built error handling using attribute [HandleError] at Class (Controller) level or Methods (Action) level.

1). In-Built way:

  1. MVC’s In-built attribute [HandleError] at Class(Controller) level or Methodd (Action) level
  2. We need <customErrors mode=”On” /> in web.config, so that by default, webserver will take us to view “/Shared/Eorror.aspx”
  3. Easily configurable
  4. No need of try…catch
  5. No logging is here

One quick reference:

http://weblogs.asp.net/scottgu/archive/2008/07/14/asp-net-mvc-preview-4-release-part-1.aspx

2). With Logging:

  1. We can create a custom attribute like [LogError] by inheriting and implementing ActionFilterAttribute, IActionFilter respectively
  2. We can decorate with both the attributes [HandleError] and [LogError]
  3. Can be logged in any of the stores like TXT/Xml/SQLServer or etc. We have to have our own classes to do this

One quick reference: http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_Filters.aspx

In the continuation, I came across one open source project called ELMAH. It’s really awesome!

3). Open Source – ELMAH (Error Logging Modules and Handlers) way:

  1. Provides the capability logging in several available persistence stores like SQLServer, Xml , Memory and etc
  2. Provides a page handler (Ex: elmah.axd)  to view the all errors in HTML form
  3. No need to add any attributes to Controller or Method.  Having a ControllerFactory, we can inject them through Application_Start() from Global.asax.cs
  4. No [HandleError] attribute, ELMAH takes care

The things need to do are:

  • Need to add ELMAH.DLL
  • Add some configuration stuffs in Web.config
  • One dummy ActionResult to redirect to Error page, which can be configured in

Urls for quick reference:

  1. http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx
  2. http://dotnetdarren.wordpress.com/2010/07/27/logging-on-mvc-part-1/ – Download the sample here
  3. http://dotnetslackers.com/articles/aspnet/ErrorLoggingModulesAndHandlers.aspx
  4. http://www.codecapers.com/post/Error-Handling-in-MVC-with-ELMAH.aspx#tb

Here are the benefits I see are:

  1. No need of try…catch block, throwing and re-throwing
  2. Provides different stores
  3. Page to view all the errors (Report) and export into different form
  4. Notification by email can be configured and too many
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: