Click here to Skip to main content
15,868,014 members
Articles / Programming Languages / C#
Article

ELMAH - Error Loggin Module and Handler For unhandled errors in asp.net

Rate me:
Please Sign up or sign in to vote.
3.78/5 (7 votes)
28 May 2008GPL33 min read 52.9K   1.1K   24   3
Error Loggin Module and Handler is an open source project used to report unhandle errors
Image 1

Introduction

No matter how well you write and test your application there are some errors which can't be avoided. The errors that occures may not be because of you code but could be cause of envoirnment, like database server shutdown, email server failure server restarted etc. Even though you take care to every thing in the live site exceptions occures. And solution for this set on higher priority. It is important to have a details record of exception in order to resolve the issue.

ELMAH – Error Logging Module and Handler which we will examine through this artical how you can use Http Modules and Handlers for logging error of your Asp.net application.

ELMAH is an application - wide exception logging system that logs unhandled exceptions. you can add ELMAH assembly in your developing project or you can configure it and add dynamically in you running Asp.net application.

The download of ELMAH is available here

Background

ELMAH is a free open source HTTP Modules and handler designed by Atif Aziz. I may not go on describing about the module much but just the basic implementation of ELMAH

Some of the features of ELMAH are listed below

  1. Logs all the unhandled exceptions
  2. Gives custome exceptions even thought the custome exception mode is off
  3. An RSS feed of the exception.
  4. Email the exceptions

Implementation

C#
protected void Button1_Click(object sender, EventArgs e)
   {
       throw new Exception ("Exception is thrown");
   }

Figure 1 shows when something like this is coded.

Image 2

Figure 1

When the user clicks on the button the error is thrown and the above screen, you can the yellow screen of death is displayed. Its the worst that a developer would expect after the project has gone to the production server.

At the most you can avoid this by creating a default custome error page when ever such unhandled error occurs.

Adding a custom error page in web.config and you would be redirected to custom error page that you have created.

XML
<customErrors mode="On" defaultRedirect="CustomeErrorPage.htm" /> 

Click on the button and see what happens, You would be redirected to you custom error page

Till here you were succesfully able to catch the unhandled exception, and redirect to you custome page. But what about the logging. The details are lost.

There is one method through which you can get the last error occured in your web app and that is through the code below.

Exception ex =  Server.GetLastError();

ELMAH Implementation

Implementing ELMAH is fairly simple.

  1. Add ELMAH assembly in your project.
  2. Configur your web.config

That is it what you need to do, below is the sample of web.config which you can refer.

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="elmah">
      <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
      <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
      <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
    </sectionGroup>
  </configSections>
  <connectionStrings >
    <add name="elmah-sql" connectionString="Data Source=xxx;Initial Catalog=ELMAH;Trusted_Connection=True" />
  </connectionStrings>
  <elmah>
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah-sql"   >
    </errorLog>
  </elmah>
  <system.web>
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
    </httpModules>
    <httpHandlers>
      <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
    <customErrors mode="On" defaultRedirect="CustomeErrorPage.htm" />
  </system.web>
</configuration>

Note: the above configuration is for error loggin in sql server. like wise you can log error XML file too. The zip file in project download consist some database scripts for tables and stored procedures. Make sure that you run this scripts before running the application.

Now execute the page, click on the button to raise error. you would be redirected to custom error page. OK till now it was fine what next ?

In order to get the details just append "/elmah.axd to you browser to the default page.

e.g. http://localhost/sitename/elmah.axd

the browser will display report something like this.Image 3

Conclusion

I hope this artical brings light to some of the simple and important loging mechanism of error loggin.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
India India
Im is a Senior Software Developer working with a software company in Mumbai(India). He has overall 5.5 years of experience in .net technology. He has knowledge in C# 3.0, SQL Server 2005, SQL Reporting service, Enterprise Library 3.0, WCSF & Windows Workflow Foundation.

He has a hands on cutting edge tool like MS Visio, Rational Rose, Borland together 2006 & CruiseControl.Net


Currently his area of interest is on LINQ and Sharepoint.

He is MCPD-EA Certified.

Comments and Discussions

 
QuestionTo the point great post Pin
Member 15453627-Jan-12 3:29
Member 15453627-Jan-12 3:29 
GeneralExample outdated Pin
Nicolai Schönberg29-Nov-10 21:25
Nicolai Schönberg29-Nov-10 21:25 
GeneralMy vote of 2 Pin
Nicolai Schönberg29-Nov-10 21:24
Nicolai Schönberg29-Nov-10 21:24 

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

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