Click here to Skip to main content
12,622,032 members (35,109 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked

Log application error details in windows event viewer

, 11 Oct 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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

Editorial Note

This articles was originally at but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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

        protected void Application_Error(object sender, EventArgs e)
                // gets the error occured
                Exception ex = Server.GetLastError().GetBaseException();

                // checks if the event source is registered
                if (!System.Diagnostics.EventLog.SourceExists(".NET Runtime"))
                    System.Diagnostics.EventLog.CreateEventSource(".NET Runtime", "Application");

                //log the details of the error occured
                System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
                log.Source = ".NET Runtime";
                log.WriteEntry(String.Format("\r\n\r\nApplication Error\r\n\r\n" +
                                             "MESSAGE: {0}\r\n" +
                                             "SOURCE: {1}\r\n" +
                                             "FORM: {2}\r\n" +
                                             "QUERYSTRING: {3}\r\n" +
                                             "TARGETSITE: {4}\r\n" +
                                             "STACKTRACE: {5}\r\n",

                // in case of exception occured in log

        protected void Application_Start(object sender, EventArgs e)

        protected void Application_End(object sender, EventArgs e)



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.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

ASP.NET Community
United States United States
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.
Group type: Collaborative Group

758 members

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 11 Oct 2013
Article Copyright 2013 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid