Click here to Skip to main content
6,936,634 members and growing! (20,191 online)
Email Password   helpLost your password?
 
Web Development » ASP.NET » General     Beginner

Global Exception Handling in ASP.NET

By xicoloko

An article on handling errors in ASP.NET Applications
C#, Windows, .NET1.0, ASP.NET, Visual-Studio, Dev
Posted:21 Feb 2002
Views:88,630
Bookmarked:55 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
16 votes for this article.
Popularity: 4.14 Rating: 3.43 out of 5
4 votes, 30.8%
1

2

3
2 votes, 15.4%
4
7 votes, 53.8%
5

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


Member
XicoLoko is a brazilian developer based in Switzerland.

Occupation: Architect
Company: VisionOne AG
Location: Switzerland Switzerland

Other popular ASP.NET articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
QuestionHelp! I've not been able to handle this error! Pinmembermnongkhlaw4:07 26 Jun '08  
GeneralPop-up Error Message in ASP.Net PinsussAnonymous19:52 30 Aug '03  
GeneralPop-up Screen PinsussAnonymous19:50 30 Aug '03  
GeneralPop-up Error Message in ASP.Net PinsussAnonymous19:49 30 Aug '03  
GeneralRe: Pop-up Error Message in ASP.Net PinmemberChetan Ranpariya21:05 13 Dec '05  
GeneralEventLog Exception Pinmemberdvcpp5:16 31 Jul '03  
GeneralRe: EventLog Exception Pinmemberrobbierad17:42 15 Jul '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 21 Feb 2002
Editor: James Spibey
Copyright 2002 by xicoloko
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project