Undocumented WCSF Feature: Global Exception Handling


There has been some traffic on the Web Client Software Factory discussion boards recently about exception handling and logging.  People are asking how to add a global exception handler to their applciations.  Rather than point folks at the discussions, I have consolidated the main points from the discussions here to explain that this is a free, undocumented feature of the WCSF. 


If you have the WCSF installed and create a new “Web Client Solution”, you get exception handling and logging.  This is implemented in a simple HttpModule in the Composite Web Application Block.  This class leverages Enterprise Library’s Exception Handling and Logging Application Blocks.  Using the web.config file for the web site and the EntLib configuration tool, you can create new Exception Handling policies, or re-configure logging. 


How does it work?


In the web.config, a HttpModule is registered:



    <httpModules>


      <add name="ExceptionLoggerHttpModule" type="Microsoft.Practices.CompositeWeb.EnterpriseLibrary.ExceptionLogger, Microsoft.Practices.CompositeWeb.EnterpriseLibrary" />


    </httpModules>


 


This HttpModule, ExceptionLogger, is as simple as you can get.  The entire class is here:



 


public class ExceptionLogger : IHttpModule


{


    public void Dispose()


    {


    }


 


    public void Init(HttpApplication context)


    {


        if (!Debugger.IsAttached)


        {


            context.Error += new EventHandler(OnUnhandledException);


        }


    }


 


    protected virtual void OnUnhandledException(object sender, EventArgs e)


    {


        HttpApplication application = (HttpApplication)sender;


        Exception ex = application.Server.GetLastError().GetBaseException();


        ExceptionPolicy.HandleException(ex, "GlobalExceptionLogger");


    }


}


 


Basically, this class registers a handler for the web application's Error event. So, any unhandled exception will be handled by the GlobalExceptionLogger exception policy that is defined in the web.config.  Out of the box, all unhandled exceptions go into the Applciaiton Event Log, with basic information. If you are debugging, this code is not triggered, and the exceptions will not be handled at all, but instead be bubbled up so you can get details and stop them from happening in the future.

Comments (8)

  1. ehoff says:

    wow..i think you just saved me a ton of work..

    i was just about to dig through about 15 web services to add global exception handling to them..i’ll just borrow this HttpModule instead 😉

    if you ever come to Nashville, I owe you a beer (err..double shot of espresso) 😉

  2. Jarle says:

    hi ehoff

    hmm. saved you a ton of work? you could just catch the exception in global.asax like the olden days and use log4net where you can configure where the destination source where the error should be logged. 😉 It’s a 5 minute job and not complicated at all.

    PS your code should be build so that no unhandled exception at all. There should be a try/catch around database, file, web service call etc. One strength (and irritation) of java is that the developer needs to explicitly handle exception from method calls that can potentially throw exception (like database connections). Unhandled exceptions are expensive and could result in you web app to shut down.  

    regards

  3. mpuleio says:

    ehoff, I’ll let you know next time I’m in Nashville so I can get that coffee. 😉 I’m glad we could help.

    Jarle,

    Yes, it is a five minute job to add a similar block of code to the Global.asax.  However, with no code changes at all this module allows the same functionality in a consistent way across any number of web apps.

    And I do agree that it is much better to never have unhandled exceptions and judiciously use try/finally and try/catch/finally blocks.  However, during development and testing, having a record of all the exceptions that slip through is a great help in making sure they don’t happen again after shipping. 😉

  4. Sam Gentile says:

    Ah Saturday morning where we can sleep in, ah no wait…I have kids to wake me up at the crack of dawn…

  5. One of the things the Web Client Software Factory provides out-of-the-box is global exception handling.

  6. Alberto Fernandez says:

    How about if i want to show in a userinterfase the unhandleException… but not with the IIS exception page, but in a different way.

    What you recommend? How can i do it with WCSF?

  7. mpuleio says:

    I’ll refer how to questions over to the WCSF discussion board: http://www.codeplex.com/websf/Thread/List.aspx

    Enjoy

  8. Ah Saturday morning where we can sleep in, ah no wait…I have kids to wake me up at the crack of dawn… Software Architecture The PAG folks continue to deliver their goodness with their first weekly drop of the new version of the Smart Client Software

Skip to main content