In the previous column, we briefly mentioned Application_Error events and Page_Error events. These events are raised whenever we have an unhandled exception occur within our application. If a Page_Error event is present within an ASP .NET page, and an unhandled error occurs, the event is raised. Application_Error is slightly different. It is raised for all unhandled exceptions in our ASP .NET application and is implemented in global.asax or within an HTTP Module.
Both of these events are extremely useful since they allow us a final opportunity to execute code before sending the caller an HTTP redirect to the named ASP .NET error page. Examples of their usefulness include logging the error to the Microsoft Windows Event Log or sending e-mail to the administrator depending upon the nature of the exception.
Writing to the Windows Event Log
The Windows Event log is useful for recording application level errors. Using the .NET classes found in the namespace System.Diagnostics, we can easily write to the Windows Event log. Below is the implementation (in Visual Basic .NET):