Click here to Skip to main content
15,868,016 members
Articles / General Programming / Exceptions
Article

Log application error details in windows event viewer

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 11.9K   1  
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

This articles was originally at wiki.asp.net 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.

 

License

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


Written By
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.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --