Global Exception Handler for Unhandled Exceptions

Hi

I come across number of posting at www.asp.net/forums for a exception handler that can handle exceptions and redirect the end user to custom errors page.

I have written below source code that catches un-recoverable exceptions and redirects the end user to custom errors page and emails the error stack to the configured email address. Note that it is quite configurable using web.config properties.

Note that below source is part of a Class Library project. It is quite feasible to use the source code in a class and modify web.config access properties to use it as you wish. You can call the methods in Applicaiton_Error() method in Global.asax file to get most out of it.

using System; 
using System.Collections.Generic; 
using System.Text;
using System.Web; 
using System.Web.Configuration; 
namespace WebUtils 
{ 
/// <summary> 
/// Appliction level exceptions are handled using this Class 
/// </summary> 
public class ExceptionHandler 
{ 
public static string EmailAddress = WebConfigurationManager.
AppSettings["EmailAddress"]; 
/// <summary> 
/// Application level unhandled exception stack trace is sent
///to the Email provided in web.config while redirecting the 
///end user to GenericErrorPage.aspx 
/// </summary> 
/// <author>Raju Golla</author> 
public static void ApplicationExceptionHandler() 
{ 
//Get exception details 
Exception ex = HttpContext.Current.Server.GetLastError(); 
if (ex is HttpUnhandledException && ex.InnerException != null) 
{ 
ex = ex.InnerException; 
} 
if (ex != null) 
{ 
try 
{ 
//Email the error stack to the administrator/relevant developer 
// const string EmailAddress = “informer@exception.com 
///<mailto:“informer@exception.com>”; 
//Create the mail message instanceSystem.Net.Mail.MailMessage 
///mm = new System.Net.Mail.MailMessage(EmailAddress, EmailAddress); 
//From||To 
//Assign message properties 
mm.Subject = WebConfigurationManager.AppSettings["EmailSubject"].ToString(); 
mm.Body = string.Format("An unhandled exception occurred:{0}Message: 
{1}{0}{0} Stack Trace:{0}{2}", System.Environment.NewLine, 
ex.Message, ex.StackTrace); 
mm.IsBodyHtml = false; 
//create SMTP client object 
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(); 
//send the error stack mail message (uses web.config SMTP
/// settings section) 
smtp.Send(mm); 
} 
//catch the Exception that is thrown when the SmtpClient is not able 
///to complete a Send peration 
catch(System.Net.Mail.SmtpException exc) 
{ 
ErrorLog.LogException(exc); 
} 
//Redirect the user to CustomErrorPage.aspx 
HttpContext.Current.Server.Transfer(WebConfigurationManager.
AppSettings["CustomErrorsPage"].ToString()); 
//("~/GenericErrorPage.aspx");
} 
} 
} 
}

References:

http://www.4guysfromrolla.com/articles/091306-1.aspx

1 Comment

  • Pretty section of content. I just stumbled upon your site and in accession capital to assert that I acquire in fact enjoyed account your blog posts. Anyway I will be subscribing to your feeds and even I achievement you access consistently rapidly.

Comments have been disabled for this content.