Global Web Application Level Error Reporting
Add Comment<span class=wboxheado>Introduction</span><br> As a web master it becomes of primary importance for you to know of any error that occurs in your Web Application. Even though good design practice calls for <b> Error Handling</b> within the application itself, still there could many un-handled errors and exceptions occurring within your web applications spoiling your clients experience.<br> The .NET Framework provides a very robust Exception Handling mechanism. But still if the developer fails to identify the area where the exception could occur, there is a high chance a large number of exceptions could be thrown if its a very frequently visited application.<br> Hence we have to have a means to be able to identify and trace the exceptions the occur within our Web Application globally.<p><span class=wboxheado>The Global.asax File</span><br> The ASP.NET runtime provides a '<b>global.asax</b>' file to handle global application events. There are a lot of things you can do within this file to handle various application level events like '<b>Application_Start</b>', '<b>Application_End</b>', '<b>Session_Start</b>', '<b>Session_End</b>' etc.. Its definitely the best place for your <b> caching</b> code for Application level resources.<br> In this example which is an adaptation of <b> Scott Guthrie's </b> example in Vb.NET. We will be using the '<b>Application_Error</b>' event to report application wide errors in our web application. The Application_Error is the event that <b> HttpApplication</b> object fires every time a error occurs in a web application. <br> Every web application in its own Virtual Directory needs one copy of Global.asax file i.e. Say you have multiple applications deployed in different directories (not virtual directories) in a Virtual Directory called 'WebApplications' then you need to place just one copy of the 'Global.asax' file in the 'WebApplications' virtual directory and it will handle all the errors that occur in any of the applications within that virtual directory.</p> <p><span class=wboxheado>Code</span><br> <b> 1) Global.asax</b> - In this example, I set the 'Global.asax' file to e-mail you every time a error occurs with the error details.</p> <table border="0" width="100%" class="code"> <tr> <td width="100%"><%@ Application Language="C#" %><br> <script runat="server" ><br> public void Application_Error(object sender, EventArgs e)<br> {<br> string mess= "Error in Path :"+Request.Path ; <span class="cmt"> //Get the path of the page</span><br> mess+="\n\n Error Raw Url :"+Request.RawUrl ; <span class="cmt"> //Get the QueryString along with the Virtual Path</span><br> <br> <span class="cmt">//Create an Exception object from the Last error that occurred on the server</span><br> Exception myError =Server.GetLastError(); <br> <br> mess+="\n\n Error Message :"+myError.Message; <span class="cmt"> //Get the error message</span><br> mess+="\n\n Error Source :"+myError.Source; <span class="cmt">//Source of the message</span><br> mess+="\n\n Error Stack Trace :"+myError.StackTrace; <span class="cmt"> //Stack Trace of the error</span><br> mess+="\n\n Error TargetSite :"+myError.TargetSite; <span class="cmt"> //Method where the error occurred</span><br> <br> <span class="cmt"> //Create a Mail Message</span><br> System.Web.Mail.MailMessage errorMessage = new System.Web.Mail.MailMessage();<br> errorMessage.To ="webmaster@yoursite.com";<br> errorMessage.From="error@yoursite.com";<br> errorMessage.Subject="Web Application Error Reporting!";<br> errorMessage.Body=mess;<br> <br> System.Web.Mail.SmtpMail.Send(errorMessage); <span class="cmt">//Send the message</span><br> }<br> </script></td> </tr> </table> <p>2) <b> Global.asax</b> - The above code is appropriate if you are hosting your web application on a shared web server. Incase you <b> own</b> the web server, you would like to add the Error information into your systems <b>EventLog</b>. The below example modifies the above example so that the error information gets added to the Event Log. <br> <b>Note:</b> In this example the Event Log is written on the same machine that hosts the web application, Event Log cannot be written remotely in an Internet environment.</p> <table border="0" width="100%" class="code"> <tr> <td width="100%"><%@ Application Language="C#" %><br> <script runat="server" ><br> string eventLogName = "WebApplicationError";<br> public void Application_Start(object sender, EventArgs e)<br> {<br> if(!System.Diagnostics.EventLog.SourceExists(eventLogName))<br> {<br> System.Diagnostics.EventLog.CreateEventSource(eventLogName, eventLogName);<br> }<br> }<br> public void Application_Error(object sender, EventArgs e)<br> {<br> string mess= "Error in Path :"+Request.Path ; <span class="cmt"> //Get the path of the page</span><br> mess+="\n\n Error Raw Url :"+Request.RawUrl ; <span class="cmt"> //Get the QueryString along with the Virtual Path</span><br> <br> <span class="cmt">//Create an Exception object from the Last error that occurred on the server</span><br> Exception myError =Server.GetLastError(); <br> <br> mess+="\n\n Error Message :"+myError.Message; <span class="cmt"> //Get the error message</span><br> mess+="\n\n Error Source :"+myError.Source; <span class="cmt">//Source of the message</span><br> mess+="\n\n Error Stack Trace :"+myError.StackTrace; <span class="cmt"> //Stack Trace of the error</span><br> mess+="\n\n Error TargetSite :"+myError.TargetSite; <span class="cmt"> //Method where the error occurred</span><br> <br> System.Diagnostics.EventLog myLog = new System.Diagnostics.EventLog(); <span class="cmt"> //Create a new EventLog object</span><br> myLog.Source=eventLogName; <span class="cmt">//Set the name of the Log</span><br> myLog.WriteEntry(mess, System.Diagnostics.EventLogEntryType.Error);<span class="cmt">//Write the log</span><br> }<br> </script></td> </tr> </table> <p>Once you save this file as 'Global.asax' and place in within the virtual directory of your Web application, the next time a error occurs in your application, the error will get recorded in your Event Log.<br> To view the Event Log on Windows 2000, go to '<b>Start -> Settings -> Control Panel -> Administrative Settings -> Event Viewer</b>'. In the Event Viewer Console, view under the '<b>Applications</b>' tab to view errors that have occurred within your Web Application.</p> <p><span class=wboxheado>Conclusion</span><br> We have created a Global Web Application Level Error Reporting System. This systems powered by the .NET Framework is very powerful, it will even log requests for Missing Urls, tweaking the above example a bit you can easily find missing links in your application.<br> I hope this example will help you solve your Administration Nightmares ..It sure has solved mine, thanks <b> Scott Guthrie</b>!</p>