TechBubbles Microsoft Technology BLOG

ASP.NET Error Handling

This article explains about adding error handling capabilities to the ASP.NET Web application. It also explores the possible ways to handle and log the errors that occur in the application.

                                             image

There are two types of exceptions that can occur in web application.

• Unhandled exceptions
• Handled exceptions

When a runtime error occurred in ASP.NET web application then you will get a yellow page. You can redirect the users to custom error page by logging the actual exception.

You can log the exception in the following data sources

• Text file
• Database
• Event Log

All Unhandled exceptions can be handled by the Application_Error event handler in global.asax file. Write the following code in event handler

 

        // Code that runs when an unhandled error occurs
        Exception objErr = Server.GetLastError().GetBaseException();
        string err = "Error in: " + Request.Url.ToString() +
                          ". Error Message:" + objErr.Message.ToString();
        // Log the error
        ErrorHandler.WriteError(err);

The above code used to log the error in text file.

To redirect the user to custom designed error page set the following options in configuration file.

<customErrors mode="On" defaultRedirect="ErrorPage.aspx" />

Instead of throwing exception which causes the burden to CPU better handle the code.

try
  {
    // Buggy code goes here
   }
catch(Exception ex)
  {  
              ErrorHandler.WriteError(ex.Message);
 }

ELMAH- Error Logging Modules And Handlers

ELMAH is a pluggable solution that can be added to a running ASP.NET Web Application with out recompilation and redeployment. It is important log the errors which occur on live website in order to assist with diagnosing the problem. ELMAH provides a mechanism for centralized error logging.

You can download ELMAH from the following link

http://code.google.com/p/elmah/

Whenever an unhandled exception occur in asp.net web application , ELMAH logs the error as specified in the web.config file.

In this way we can send more user-friendly messages to the users whenever 404,500 or runtime errors occur.

Adding ELMAH to ASP.NET Web application

1. Add the ELMAH assembly to the web application.

2. Configure the Web application to use ELMAH’s HTTP modules and HTTP handlers.

In the Web.config file you will need to add the following settings:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <!-- Allows for a new section group to the Web.config -->
    <sectionGroup name="gotdotnet.elmah">
      <!-- Indicates that inside the section group there will be an
              errorLog section -->
      <section name="errorLog" 
        type="System.Configuration.SingleTagSectionHandler, 
          System, Version=1.0.5000.0, Culture=neutral, 
          PublicKeyToken=b77a5c561934e089" />
    </sectionGroup>
  </configSections>
  <!-- This section group contains the type of the exception logger
         to use (SqlErrorLog, MemoryErrorLog, or a custom logger).
         It also contain properties pertinent to the exception logger
         (connectionString, for the SqlErrorLog). -->
  <gotdotnet.elmah>
    <errorLog type="GotDotNet.Elmah.SqlErrorLog, 
      GotDotNet.Elmah, Version=1.0.5527.0, Culture=neutral, 
      PublicKeyToken=978d5e1bd64b33e5" 
      connectionString="...connection string..." />
  </gotdotnet.elmah>
  <system.web>
    <!-- Register that a request to aspnetham/errorlog.aspx should
        be serviced by the ErrorLogPageFactory HTTP Handler factory -->
    <httpHandlers>
      <add verb="POST,GET,HEAD" path="elmah/default.aspx" 
        type="GotDotNet.Elmah.ErrorLogPageFactory, 
        Skybow.Samples.AspNetHam, Version=1.0.5527.0, 
        Culture=neutral, PublicKeyToken=978d5e1bd64b33e5" />
    </httpHandlers>
    <!-- Adds the ErrorLogModule HTTP Module to the HTTP pipeline. -->
    <httpModules>
      <add name="ErrorLog" type="GotDotNet.Elmah.ErrorLogModule, 
         GotDotNet.Elmah, Version=1.0.5527.0, Culture=neutral, 
         PublicKeyToken=978d5e1bd64b33e5" />
    </httpModules>
    
    ...
  </system.web>
</configuration>

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

Add Comment

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud