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:
="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