Click here to Skip to main content
15,511,353 members
Articles / .NET
Posted 29 Apr 2011


24 bookmarked

Exception logging using Elmah - Error Logging Modules and Handlers for ASP.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
29 Apr 2011CPOL2 min read
Exception logging using Elmah - Error Logging Modules and Handlers for ASP.NET

This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise products in any way, shape or form. Please report any spam or advertising.


Exception logging is one of the most important parts of application maintenance. There are many open source logger applications which we can use in our application to log exception details into a central repository, send exception notification to specific people. Elmah - Error Logging Modules and Handlers is one of those. For details about Elmah, please visit this link and for details on how to use it, please visit this link.


In this article, I would also do the same thing, i.e., discuss about how to use Elmah in ASP.NET application. Before I start, we need to do some ground work, for instance, download the source code of Elmah from here. Then I created a database named Elmah_Errorlog into the Microsoft SQL Server; for this instance I use SQL 2008. That's all for the database for now. We will create the Table and Stored Procedure later on. I unzipped the Elmah source code and open the solution via Visual Studio. After opening the Elmah via Visual Studio, there is a file named SQLServer.sql, open this file and from this file I copied the following table and stored procedure creation SQL code and run into the SQL server query window to create table and stored procedure for Elmah_Errorlog database.

CREATE TABLE[dbo].[ELMAH_Error](........) 
CREATE PROCEDURE [dbo].[ELMAH_GetErrorXml](.........)
CREATE PROCEDURE [dbo].[ELMAH_GetErrorsXml](.......)
CREATE PROCEDURE [dbo].[ELMAH_LogError](........ 

After creating the database, the schema will show like below:

Image 1

Using the Code

So I have created my database for Elmah, now I am going to create Test Harness project to show how to use Elmah to log error. In Elmah solution, I created a Web project named Elmah-TestHarness, this is a simple ASP.NET web application with only Default.aspx page. I added Elmah project as reference to this test harness to consume Elmah.dll.

The most important thing to do is modify the Web.Config file of the Elmah-TestHarness web project to use Elmah. The contents of the web.config are as below:

<?xml version="1.0"?>
    <sectionGroup name="elmah">
      <section name="security" requirePermission="false"
	type="Elmah.SecuritySectionHandler, 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" />
    <add name="elmah-express" connectionString="Server=KAZOL-PC\SQLEXPRESS08;
	Database=Elmah_Errorlog;Trusted_Connection=Yes;" />
    <errorLog type="Elmah.SqlErrorLog, Elmah"
             connectionStringName="elmah-express" />
        <equal binding="HttpStatusCode" value="404" valueType="Int32" />
    <compilation debug="true" targetFramework="4.0" />
      <add verb="POST,GET,HEAD" path="elmah.axd"
	type="Elmah.ErrorLogPageFactory, Elmah" />
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />

and the contents of the Default.aspx file are below:

<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="Elmah_TestHarness._Default" %>

    <title>Elmah - Test Harness</title>
    <form id="frmMain" runat="server">
        Elmah Test Harness</h1>
        <legend>Elmah Test harness controls</legend>
        <asp:Button ID="btnTest" runat="server" 
	OnClick="btnTest_Click" Text="Exception Test" />

The code behind of the Default.aspx page is as below:

 namespace Elmah_TestHarness
    using System;

    public partial class _Default : System.Web.UI.Page
        protected void btnTest_Click(object sender, EventArgs e)
            throw new Exception("test");

Now that everything is ready to go, click on the F5 (make sure Elmah_TestHarness project has been set as Startup app in the solution) and I see the following output:

Image 2

Clicking on the Exception Test button will throw an exception and Elmah from the behind will catch that and log into the Elmah_Errorlog database. Elmah also exposes a HttpHandler which is for seeing the exception details. another tab of Chrome writes the below URL localhost:28350/Elmah.asxd then it will show up as below:

Image 3


  • 30th April, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
Australia Australia

Comments and Discussions

QuestionConnection string is encrypted Pin
Member 230643312-Nov-11 23:36
Member 230643312-Nov-11 23:36 
AnswerRe: Connection string is encrypted Pin
Mohammad A Rahman13-Nov-11 0:13
Mohammad A Rahman13-Nov-11 0:13 

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.