Global.asax, is the global file in the web application, which offers application level events to be registered. There are many of the events in this file which can be used for specific purposes. "Application_Error" is the one which is fired when some unhandled exception occurs in the application. It is very significant in this basic way that if that unhandled exception is not handled in this event then the exception summary page will be shows to the user of the application. Which would perplex him, leaving him uncertain about the application. So, it's always good to handle unhandled exceptions in this event.
This article would focus not only on handling the exception in the "Application_Error" event, but also to log the exception detail in the "Event Viewer".
public class Global : System.Web.HttpApplication<br /> {<br /><br /> protected void Application_Error(object sender, EventArgs e)<br /> {<br /> try<br /> {<br />
The code in the "Application_Error" event, first retrieves the currently occurred exception from server object, to write its details in the Event Viewer Log. "System.Diagnostics.EventLog" makes it possible to write the log details in the Event Viewer. It creates the event source, for writing the event messages in case the source does not exist already, and then using the object of EventLog, it writes the formatted detail message in the event viewer. "Server.ClearError()" clears the previously occurred exception.
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.
The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.