Click here to Skip to main content
Click here to Skip to main content

Global Exception Handling in ASP.NET

, 21 Feb 2002
Rate this:
Please Sign up or sign in to vote.
An article on handling errors in ASP.NET Applications

Sample Image

Introduction

This text was based on the article "Global Exception Handling with ASP.NET" found at webdeveloper.earthweb.com

So, you've just finished your Web Application using ASP.NET. Everythings seems to be working fine. But how to be notified if an error happens while the site is running? A solution to this problem is shown in this article.

Handling Errors in a ASP.NET application

First of all you must catch the unhandled exceptions that happen in your Web Application by handling the HttpApplication Error event. You accomplish this task by editing your Global.asax file as follow:

protected void Application_Error(object sender, EventArgs e)
{
	// excpt is the exception thrown
	// The exception that really happened is retrieved through the GetBaseException() method
	// because the exception returned by the GetLastError() is a HttpException
	Exception excpt = Server.GetLastError().GetBaseException();

}

How to be notified about those unhandled exceptions?

The 3 most common ways to be notified about those "programming mistakes" are: File Log, EventLog and Email. As you would imagine I wrote a simple class that does this notifing stuff according to your Web.Config settings. You can see below a sample Web.Config file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <!-- application specific settings -->
  <appsettings>
	  	<!-- here you define the way you want to be notified -->
	  	<!-- LogEvent = 1, FileLog = 2 e Email = 4. Use 0 when you don't want to be notified -->
      	<add key="errorNotifier_NotifyMode" value="3" />

    	<!-- Here you define the event source entry in your LogEvent, the default value is ASP.NET App Error -->
      	<add key="errorNotifier_EventLogSource" value="Sample App error" />

     	<!-- Here you define the full path where the file logging should occur -->
     	<add key="errorNotifier_Filename" value="error.txt" />

     	<!-- The email address that will receive the notifications -->
     	<add key="errorNotifier_EmailTo" value="error@mydomain.com" />

     	<!-- The email account that sends the emails -->
     	<add key="errorNotifier_EmailFrom" value="errornotifier@mydomain.com" />

     	<!-- The email subject -->
     	<add key="errorNotifier_EmailSubject" value="Error in ASP.NET app" />

     	<!-- The SmtpServer address. Just use if you don't want to use the local SmtpServer -->
     	<add key="errorNotifier_EmailSmtpServer" value="mySmtpServerAddress" />

  </appsettings>

  <system.web>

Now change the Application_Error event implementation in your Global.asax as something as:

protected void Application_Error(object sender, EventArgs e)
{
	XC.Web.ErrorHandler handler = new XC.Web.ErrorHandler();

	// if you wish you can change the settings by now
	// handler.EmailSettings.To = "me@domain.com";

	handler.HandleException();
}

You can also make you Web Application show a special page whenever an error happens, giving a better feedback to your users. To do it, edit your Web.Config file as follow:

<system.web>
	...
	<customerrors mode="Off" defaultRedirect="~/myErrorPage.aspx" />
	...
</system.web>

Possible values to mode:

  • On : you want to handle errors
  • RemoteOnly : the errors will be handled only when they happen in a box differents than the IIS's. That's useful for debugging
  • Off : you don't want to handle errors at all

The defaultRedirect value is the url that should be shown in case of a unhandled exception

It's important to note that the ASP.NET user must have the right to write to the EventLog in case this method of notification is used. Also it must have write permission to the directory of the log file, if apply.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

xicoloko
Architect VisionOne AG
Switzerland Switzerland
XicoLoko is a brazilian developer based in Switzerland.

Comments and Discussions

 
QuestionHelp! I've not been able to handle this error! Pinmembermnongkhlaw26-Jun-08 3:07 
URL : http://www.codeproject.com/KB/aspnet/%20/ErrorHandlingASPNET.aspx
 
Note the %20 I deliberately inserted in the URL. I got :
 
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
 
Requested URL: /KB/aspnet/ /ErrorHandlingASPNET.aspx
 
Question : How do I handle such types of errors to display me a friendlier page?
GeneralPop-up Error Message in ASP.Net PinsussAnonymous30-Aug-03 18:52 
GeneralPop-up Screen PinsussAnonymous30-Aug-03 18:50 
GeneralPop-up Error Message in ASP.Net PinsussAnonymous30-Aug-03 18:49 
GeneralRe: Pop-up Error Message in ASP.Net PinmemberChetan Ranpariya13-Dec-05 20:05 
GeneralEventLog Exception Pinmemberdvcpp31-Jul-03 4:16 
GeneralRe: EventLog Exception Pinmemberrobbierad15-Jul-04 16:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 22 Feb 2002
Article Copyright 2002 by xicoloko
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid