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:
<add name="ExceptionLoggerHttpModule" type="Microsoft.Practices.CompositeWeb.EnterpriseLibrary.ExceptionLogger, Microsoft.Practices.CompositeWeb.EnterpriseLibrary" />
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)
context.Error += new EventHandler(OnUnhandledException);
protected virtual void OnUnhandledException(object sender, EventArgs e)
HttpApplication application = (HttpApplication)sender;
Exception ex = application.Server.GetLastError().GetBaseException();
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.