Global Exception Handling in the Web Client Software Factory
by David Hayden ( Microsoft MVP C# ), Filed: Web Client Software Factory
Michael Puleio schooled me on this in the Web Client Software Factory Forums so I had to share this with the world because it may not be obvious.
The Web Client Software Factory comes with a bit of plumbing pre-configured for you. Some of this can be exposed by using the Enterprise Library Configuration Tool to get a nice graphical representation of the Web.config file that is intially built by WCSF:

The image above shows that the Web Client Software Factory is using the Exception Handling Block in Enterprise Library to log messages to the EventLog via the Logging Application Block. The well-named exception policy, GlobalExceptionLogger, implies that the Web Client Software Factory is hooking into the HttpApplication.Error event to log unhandled exceptions. The question is how?
ExceptionLoggerHttpModule
Further examination of the Web.config file shows the existence of an HttpModule, called ExceptionLogger HttpModule:
<httpModules>
<add name="ExceptionLoggerHttpModule" type="Microsoft.Practices
.CompositeWeb.EnterpriseLibrary.ExceptionLogger,
Microsoft.Practices.CompositeWeb.EnterpriseLibrary" />
</httpModules>
The module is the answer. It is how the Web Client Software Factory hooks into the pipeline and attaches itself to the HttpApplication.Error Event to log unhandled exceptions :)
Debugger.IsAttached Suppressing Log Messages While Debugging
What made me originally think the Web Client Software Factory wasn't logging unhandled global exceptions was the fact that I purposely caused exceptions in my application during debugging and nothing was getting logged to the EventLog. Nothing.
The source code to the ExceptionLogger HttpModule shows that unhandled exceptions are not logged when debugging in the IDE. The check for !Debugger.IsAttached avoids logging while debugging:
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");
}
}
So that explains why unhandled exceptions won't be logged while you are debugging your applications :)
Conclusion
Good stuff. Again, thanks to Michael for mentioning the existence of the ExceptionLogger HttpModule which I totally missed in the Web.config file.
Source: David Hayden ( Microsoft MVP C# )
Filed: Web Client Software Factory