views:

1972

answers:

3

I've been given the thrilling task of re-writing our exception handling system. Whilst I will state that handling exceptions from an application-wide point of view isn't something we want, typically it's unavoidable when our team are understaffed for the sheer amount of work we need to push out the door, so please, no flaming the globalised solution to exception handling here :)

I've had a good hunt to see what common solutions exist. At the moment we use Global.asax with the Application_Error event to do Server.GetLastError() which is placed in Session state then a redirect is called to another page where the session data is then retrieved and output in a human readable format. The redirect also calls a sproc which will carefully audit the error information which is a) e-mailed to the developers and b) viewed from a web page only viewable by developers.

The new way I've seen of doing things is using the IHttpModule interface using a class in App_Code to do something along these lines (this is my quick implementation)

Imports Microsoft.VisualBasic

Public Class ErrorModule : Implements IHttpModule

  Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
    ' Not used
  End Sub

  Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
    AddHandler context.Error, AddressOf context_Error
  End Sub

  Public Sub context_Error(ByVal sender As Object, ByVal e As EventArgs)
    Dim ex As Exception = HttpContext.Current.Server.GetLastError

    ' do something with the error
    ' call the stored procedure
    ' redirect the user to the error page

    HttpContext.Current.Server.ClearError()
    HttpContext.Current.Response.Redirect("index.htm")

  End Sub
End Class

My question is, what is the benefit of this solution over using Global.asax events? Additionally, what is the best way to hand the data to an error page?

EDIT: The code above does work by the way ;)

EDIT: Also, how does the HttpModule work behind the scenes? Does it just register the Error event to that particular function on application start?

UPDATE:

Upon much further investigation it seems grabbing session data is really, really messy when it comes to using IHttpModule interface. I don't think MS have matured HttpModule enough for it to be used in our particular scenario - until there are events specific to session data it's too dangerous for us to use.

A: 

HttpModule basically does the same thing as Global.asax. It's designed as a more reusable and self-contained module for event handling.

Mehrdad Afshari
+3  A: 

Using a module has the advantage of being easily removable, all you need to do to disable it is to remove it from <httpModules> in your config.

As far as your data goes, try going with Server.Transfer or Server.RewritePath - that will keep all the current data (including the last server error).

If for some reason it clears the last error, you can save the error to HttpContext.Items before the transfer/rewrite and then retrieve it afterwards.

Edit: In response to your edit, an IHttpModule attaches to any appropriate events in it's IHttpModule.Init implementation.

Richard Szalay
Can the HttpModule be used in place of Global.asax? Or does Global.asax have to always exist, even if it's not doing anything?
Kezzer
Global.asax does not need to exist. If you don't use it the default HttpApplication class will be used (instead of your inherited class in Global.asax)
Richard Szalay
I can't seem to find anything that correlates to Session_Start in HttpModule though - is it not supported?
Kezzer
You can access that event via: ((SessionStateModule)application.Modules["Session"]).Start
Richard Szalay
A: 

I asked a related question a few days ago.

See http://stackoverflow.com/questions/583307/asp-net-error-handling

NYSystemsAnalyst
My apologies for missing this, I did have a search beforehand but hadn't foudn this. Thanks!
Kezzer