Writing Error Handlers for an ASP.NET application has been made really simple using the Global.asax and the Application_Error event. This article demonstrates a generic error handler in the global.asax which will be implemented for any application error generated by the web application for which a custom error handler has not been written.
There are three parts to this error handling routine
1. It writes the error message to the event log
2. It sends the development team an email with the error details
3. It has a ‘pretty’ message on the web page, letting the user know that an error occurred and that the development team has been notified.
We are going to use the Application_Error event of the global.asax file. The HttpApplication class in the System.Web namespace implements this Error event handler, so make sure to add a reference to this namespace. We also need to add a reference to the System.Diagnosistics namespace
--- protected void Application_Error(Object sender, EventArgs e) {
// declare an exeception object and get the original exception
Exception exception = Server.GetLastError().GetBaseException();
// now we are going to be nice to ourselves and format that stack trace.
// It would be easy to just do a string stackTrace = exception.StackTrace, but after
// you’ve tried to read a few of these spending time formatting it – is time well spent
string stackEntryDelimiter = " at ";
string topStackEntry = String.Empty;
string stackTrace = exception.StackTrace;
try {
int nextStackEntry = stackTrace.IndexOf(stackEntryDelimiter, stackEntryDelimiter.Length);
if(nextStackEntry > 0) {
topStackEntry = stackTrace.Substring(0, nextStackEntry);
}
else {
topStackEntry = stackTrace;
}
}
catch(Exception ex) {
// if for whatever reason our error handler decides to generate an error – well we
just eat it
Debug.Write(ex.Message);
}
try {
string eventLogFormat = "Error in {0} User: {1} Error Message: {2} Line: {3}";
string[] eventLogArgs = {
Request.Url.ToString(),
Context.User.Identity.Name,
exception.Message,
topStackEntry
};
string eventLogMessage = String.Format(eventLogFormat, eventLogArgs);
EventLog objLog = new EventLog();
objLog.Source = "appname";
// change this to the application name for which you’re trapping errors
objLog.WriteEntry(eventLogMessage, EventLogEntryType.Error);
// write to the event log
}
catch(Exception ex) {
// just eat it
Debug.Write(ex.Message);
}
try {
// Again we want to format our error message in the email
string mailFormat = "<HTML>" +
"Error at: {0}<BR>" +
"User: {1}<BR>" +
"Error Message: {2}<BR><BR>" +
"Stack Trace <HR><BR>{3}" +
"";
string[] mailArgs = {
Request.Url.ToString(),
Context.User.Identity.Name,
exception.Message,
stackTrace.Replace(stackEntryDelimiter, "<BR>").Remove(0,6)
};
string mailMessage = String.Format(mailFormat, mailArgs);
EmailReport emailReport = new EmailReport(sourceEmailAddress,
DevTeamEmailAddress,
"appName Error on " + Server.MachineName,
mailMessage,
"");
emailReport.SendEmail();
}
catch(Exception ex) {
// just eat this one too..
Debug.Write(ex.Message);
}
// clear the error and put a custom message on the page
Server.ClearError();
Response.Write("We're Sorry...");
Response.Write("An error has occured on the page you were requesting.
Your System Administrator has been notified <BR>");
Response.Write("Url: " + Request.Url.ToString() + "<BR>");
Response.Write("Err: " + exception.Message + "<BR>");
}
---
A few things to make sure of
1. Be sure that SMTP is enabled on your web server, otherwise you just might not get any emails
2. Make sure the ASP.NET user has the appropriate permission to write to the event log. This requires a registry tweak.
Create a key with the name of your application under HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Evenlog\Application\) and grant security permissions to ASPNET user. Underneath this key create a new string value named EventMessageFile and give the string the path to the message dll. This should be c:\winnt\microsoft.net\framework\v1.0.3705\EventLogMessages.dll if .NET was installed using the default.
by Llama Lopon