protected void Application_Error(Object sender, EventArgs e)
{
WebErrorHandler Handler = new WebErrorHandler(Server.GetLastError().InnerException);
// *** If a log file is specified we want to log the error.
if (App.Configuration.ErrorLogFile != "")
{
Handler.LogFileName = App.Configuration.ErrorLogFile;
Handler.LogError(true);
}
// *** Retrieve the detailed String information of the Error
string ErrorDetail = Handler.ToString();
// *** Optionally email it to the Admin contacts set up in WebStoreConfig
if (App.Configuration.SendAdminEmail)
WebStoreUtils.SendAdminEmail(App.Configuration.StoreName + "Error: " + Request.RawUrl,ErrorDetail);
// *** Debug modes handle different error display mechanisms
// *** 0 - default ASP.Net - depends on web.config settings
// *** 1 - display a generic application error message with no error info
// *** 2 - display a detailed error message with full error info independent of web.config setting
if (App.Configuration.DebugMode == 2)
{
Server.ClearError();
MessageDisplay.DisplayMessage("Application Error",ErrorDetail );
return;
}
else if (App.Configuration.DebugMode == 1)
{
//Response.Clear();
Server.ClearError();
MessageDisplay.DisplayMessage("Application Error",
"We're sorry, but an unhandled error occurred on the server. " +
"The Server Administrator has been notified and the error logged." +
"Please continue on by either clicking the back button and retrying your request or by returning to the home page." +
"='" + App.Configuration.StoreBaseUrl + "'>Web Store Home Page");
return;
}
return;
}
I use a configuration class that holds a number of settings (stored in Web.Config) that determine how errors are managed. One of the settings ni there allows switching debug modes, so I can have default ASP.Net error handling, a custom application error (deployment mode) or a developer error message that displays information much like ASP.Net's default, but has additional info (and also does it regardless of the RemoteOnly setting in web.config - so I can see what's wrong remotely without editing web.config).
If this sounds interesting you can take a look at the intefaces of these classes in the West Wind Web Store Documentation:
http://www.west-wind.com/westwindwebstore/docs/
Click on Class Reference Utility Classes | Class wwWebErrorHandler...
There's a bunch of information on error handling in general and then specifically to using this class.
Regards,
>I have TRY...CATCH blocks scattered at appropriate places throughout my application, but would like to have some kind of global exception handling in place to catch and log other, otherwise unhandled exceptions.
>
>If this was a windows application, I'd wrap the instantiation of the application object and launching of the application in a TRY...CATCH block from the Main() method, but I don't know how to do the equivalent in an ASP.NET application.
>
>Thanks.+++ Rick ---
West Wind Technologies
Maui, Hawaii
west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web
Where do you want to surf today?