Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


Global Web Application Level Error Reporting

Add Comment
 

 
Introduction
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 Error Handling within the application itself, still there could many un-handled errors and exceptions occurring within your web applications spoiling your clients experience.
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.
Hence we have to have a means to be able to identify and trace the exceptions the occur within our Web Application globally.

The Global.asax File
The ASP.NET runtime provides a 'global.asax' 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 'Application_Start', 'Application_End', 'Session_Start', 'Session_End' etc.. Its definitely the best place for your caching code for Application level resources.
In this example which is an adaptation of Scott Guthrie's example in Vb.NET. We will be using the 'Application_Error' event to report application wide errors in our web application. The Application_Error is the event that HttpApplication object fires every time a error occurs in a web application. 
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.

Code
1) Global.asax - In this example, I set the 'Global.asax' file to e-mail you every time a error occurs with the error details.

<%@ Application Language="C#" %>
<script runat="server" >
public void Application_Error(object sender, EventArgs e)
{
  string mess= "Error in Path :"+Request.Path ; //Get the path of the page
  mess+="\n\n Error Raw Url :"+Request.RawUrl ; //Get the QueryString along with the Virtual Path

  //Create an Exception object from the Last error that occurred on the server
  Exception myError =Server.GetLastError(); 

  mess+="\n\n Error Message :"+myError.Message; //Get the error message
  mess+="\n\n Error Source :"+myError.Source;  //Source of the message
  mess+="\n\n Error Stack Trace :"+myError.StackTrace; //Stack Trace of the error
  mess+="\n\n Error TargetSite :"+myError.TargetSite; //Method where the error occurred

  //Create a Mail Message
  System.Web.Mail.MailMessage errorMessage = new System.Web.Mail.MailMessage();
  errorMessage.To ="webmaster@yoursite.com";
  errorMessage.From="error@yoursite.com";
  errorMessage.Subject="Web Application Error Reporting!";
  errorMessage.Body=mess;

  System.Web.Mail.SmtpMail.Send(errorMessage); //Send the message
}
</script>
 

2) Global.asax - The above code is appropriate if you are hosting your web application on a shared web server. Incase you own the web server, you would like to add the Error information into your systems EventLog. The below example modifies the above example so that the error information gets added to the Event Log. 
Note: 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.

<%@ Application Language="C#" %>
<script runat="server" >
string eventLogName = "WebApplicationError";
public void Application_Start(object sender, EventArgs e)
{
  if(!System.Diagnostics.EventLog.SourceExists(eventLogName))
  {
     System.Diagnostics.EventLog.CreateEventSource(eventLogName, eventLogName);
  }
}
public void Application_Error(object sender, EventArgs e)
{
  string mess= "Error in Path :"+Request.Path ; //Get the path of the page
  mess+="\n\n Error Raw Url :"+Request.RawUrl ; //Get the QueryString along with the Virtual Path

  //Create an Exception object from the Last error that occurred on the server
  Exception myError =Server.GetLastError(); 

  mess+="\n\n Error Message :"+myError.Message; //Get the error message
  mess+="\n\n Error Source :"+myError.Source;  //Source of the message
  mess+="\n\n Error Stack Trace :"+myError.StackTrace; //Stack Trace of the error
  mess+="\n\n Error TargetSite :"+myError.TargetSite; //Method where the error occurred

  System.Diagnostics.EventLog myLog = new System.Diagnostics.EventLog(); //Create a new EventLog object
  myLog.Source=eventLogName;  //Set the name of the Log
  myLog.WriteEntry(mess, System.Diagnostics.EventLogEntryType.Error);//Write the log
}
</script>
 

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.
To view the Event Log on Windows 2000, go to 'Start -> Settings -> Control Panel -> Administrative Settings -> Event Viewer'. In the Event Viewer Console, view under the 'Applications' tab to view errors that have occurred within your Web Application.

Conclusion
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.
I hope this example will help you solve your Administration Nightmares ..It sure has solved mine, thanks Scott Guthrie!

Comments

Add Comment