The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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
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:
<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;
<errorLog type="Elmah.SqlErrorLog, Elmah"
<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:
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:
- 30th April, 2011: Initial post