Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Application level exception handling
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00833443
Message ID:
00833653
Views:
22
Hi Steve,

AS you've already found out the place to do this is APPLICATION_ERROR in your global.asax file. This method gets routed each error and you can then look at each type.

It's quite useful actually as you can selectively decide what to do with errors - you can ignore them, clear them and display another page (most common scenario) and of course it's also your hook to log and email error information.

Since this comes up in every up I've created a Error logging class that retrieves all sorts of information about the request, parses it, optionally logs and emails. With it here's what a typical Application_error looks like:
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.<p>" +
			"Please continue on by either clicking the back button and retrying your request or by returning to the home page.<p>" + 
			"='" + 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?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform