Click here to Skip to main content
13,251,998 members (66,687 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 11 Oct 2013

Log application error details in windows event viewer

, 11 Oct 2013
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<br />    {<br /><br />        protected void Application_Error(object sender, EventArgs e)<br />        {<br />            try<br />            {<br />                // gets the error occured<br />                Exception ex = Server.GetLastError().GetBaseException();<br /><br />                // checks if the event source is registered<br />                if (!System.Diagnostics.EventLog.SourceExists(".NET Runtime"))<br />                {<br />                    System.Diagnostics.EventLog.CreateEventSource(".NET Runtime", "Application");<br />                }<br /><br />                //log the details of the error occured<br />                System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();<br />                log.Source = ".NET Runtime";<br />                log.WriteEntry(String.Format("\r\n\r\nApplication Error\r\n\r\n" +<br />                                             "MESSAGE: {0}\r\n" +<br />                                             "SOURCE: {1}\r\n" +<br />                                             "FORM: {2}\r\n" +<br />                                             "QUERYSTRING: {3}\r\n" +<br />                                             "TARGETSITE: {4}\r\n" +<br />                                             "STACKTRACE: {5}\r\n",<br />                                             ex.Message,<br />                                             ex.Source,<br />                                             Request.Form.ToString(),<br />                                             Request.QueryString.ToString(),<br />                                             ex.TargetSite,<br />                                             ex.StackTrace),<br />                                             System.Diagnostics.EventLogEntryType.Error);<br /><br />                Server.ClearError();<br />            }<br />            catch<br />            {<br />                // in case of exception occured in log<br />                Server.ClearError();<br />            }<br />        }<br /><br />        protected void Application_Start(object sender, EventArgs e)<br />        {<br /><br />        }<br />        protected void Application_End(object sender, EventArgs e)<br />        {<br /><br />        }<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.



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
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Collaborative Group

764 members

You may also be interested in...


Comments and Discussions

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