Click here to Skip to main content
Click here to Skip to main content

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

, 29 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Exception logging using Elmah - Error Logging Modules and Handlers for ASP.NET

Editorial Note

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.

Introduction

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.

Background

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:

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"?>
<configuration>
  <configSections>
    <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" />
    </sectionGroup>
  </configSections>
  <connectionStrings>
    <add name="elmah-express" connectionString="Server=KAZOL-PC\SQLEXPRESS08;
	Database=Elmah_Errorlog;Trusted_Connection=Yes;" />
  </connectionStrings>
  <elmah>
    <errorLog type="Elmah.SqlErrorLog, Elmah"
             connectionStringName="elmah-express" />
    <errorFilter>
      <test>
        <equal binding="HttpStatusCode" value="404" valueType="Int32" />
      </test>
    </errorFilter>
  </elmah>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpHandlers>
      <add verb="POST,GET,HEAD" path="elmah.axd"
	type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
    </httpModules>
  </system.web>
</configuration>

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

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

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

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:

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:

History

  • 30th April, 2011: Initial post

License

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

Share

About the Author

Mohammad A Rahman
Software Developer
Australia Australia
Designer and Architect.
Author of the Expert C# 5.0: with the .NET 4.5 Framework book

Comments and Discussions

 
QuestionConnection string is encrypted PinmemberMember 230643312-Nov-11 22:36 
AnswerRe: Connection string is encrypted PinmemberMohammad A Rahman12-Nov-11 23:13 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 30 Apr 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid