Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global Exception Handling in ASP.NET
03-07-2012, 04:01 AM
Post: #1
Global Exception Handling in ASP.NET
Hi to All,

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();


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.

<!-- 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 -->

<!-- Here you define the event source entry in your LogEvent, the default value is ASP.NET App Error -->

<!-- Here you define the full path where the file logging should occur -->

<!-- The email address that will receive the notifications -->

<!-- The email account that sends the emails -->

<!-- The email subject -->

<!-- The SmtpServer address. Just use if you don't want to use the local SmtpServer -->

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";


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:



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


User(s) browsing this thread: 1 Guest(s)