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

Exception logging in ASP.NET applciation

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

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 MS SQL Server for this instance I use SQL 2008. Thats 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  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](........)
so after creating the database, the schema will show like below,

Fig: Elmah_Errorlog database schema and details

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 is 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=xxxxx-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 is 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");

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

Fig: Elmah Test Harness.

Click on the Exception Test button will throw a exception and Elmah from the behind will catch that and log into the Elmah_Errorlog database. Elmah also expose a HttpHandler which is for to see the exception details. another tab of Chrome write the below URL localhost:28350/Elmah.asxd then it will show up as below,

Fig: Elmah Http Handler output.

On my next exception related article I will discuss about central error logging for applications using WCF.

Source code of the above article is here.

thanks mohammad.

 Few C# and Application Design books from Amazon,

1 comment:

  1. This is a great logging library: