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
Anonymous
February 21, 2007
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) ;)Anonymous
February 21, 2007
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. regardsAnonymous
February 21, 2007
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. ;-)Anonymous
February 24, 2007
The comment has been removedAnonymous
February 26, 2007
The comment has been removedAnonymous
February 27, 2007
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?Anonymous
March 01, 2007
I'll refer how to questions over to the WCSF discussion board: http://www.codeplex.com/websf/Thread/List.aspx EnjoyAnonymous
December 03, 2008
The comment has been removed