A quick glance at Microsoft Enterprise Library 2.0 - Logging Unhandled Exceptions
Let’s use the Enterprise Library 2.0 Logging Application Block in an ASP.NET 2.0 Website using the most simplistic, yet very important case of logging unhandled exceptions. If you log nothing else in your ASP.NET websites, make sure you log unhandled exceptions so that you have some idea of the health of your application. Enterprise Library 2.0 can email you the exception if you want, but I am going to keep things real simple and just have the exceptions logged to a file on the webserver.
Enterprise Library Configuration Tool
Once I create my website in Visual Studio 2005, I immediately open up the Enterprise Library Configuration Tool and use it to open the existing web.config file. Using only the tool, I then add the logging application block configuration section to web.config, remove all the defaults, and replace them with a simple FlatFile TraceListener that catches all events as shown below:

The Logging Application Block has a Special Source, called All Events. Hooking a TraceListener to this event guarantees you will receive all events not being filtered. Since we have no filters, we get all events. These events get logged to the FlatFile TraceListener, which points to a log file at the root of my website, called trace.log ( default name ).
Catching and Logging Unhandled ASP.NET Exceptions
To catch unhandled exceptions in ASP.NET 2.0, I need to do a couple of things.
First, I need to reference Microsoft.Practices.EnterpriseLibrary.Logging in my website. When you reference this library, it will also pull over a couple of other assemblies, Common and ObjectBuilder, that it depends upon.
Second, I need to add a Global Application File ( Global.asax ) to the website and enter the following code into Application_Error:
<%@ Application Language="C#" %><%@ Import Namespace="Microsoft.Practices. EnterpriseLibrary.Logging" %> <script runat="server"> void Application_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError().GetBaseException(); string message = ex.Message + "\nSOURCE: " + ex.Source + "\nFORM: " + Request.Form.ToString() + "\nQUERYSTRING: " + Request.QueryString.ToString() + "\nTARGETSITE: " + ex.TargetSite + "\nSTACKTRACE: " + ex.StackTrace; Logger.Write(message); } </script>
Logger is the name of the facade in the Logging Application Block to log messages. I grab the unhandled exception and send it out to be logged. The application doesn’t know and doesn’t care where; it is oblivious as to the actual logging strategy.
Creating An Unhandled Exception
Let’s open up Default.aspx.cs file and make it throw an exception:
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int error = Int32.Parse("bbbbb"); }}
The above with throw a System.FormatException error which will be logged in the trace.log file when running the application:
Timestamp: 2/15/2006 9:23:30 PM
Message: Input string was not in a correct format…<Stack Trace>
Conclusion
In about 15 minutes, you can successfully log unhandled exceptions in your ASP.NET 2.0 websites using Enterprise Library 2.0 Logging Application Block.
Source: David Hayden ( .NET Developer )
