Error logging using Elmah

INTRODUCTION
Developers mostly use the third party controls for logging errors in asp.net applications or some may write custom code for logging errors.

In this article I will mentioned such third party control for logging errors. It is very handy, easy to use and useful control for logging errors in asp.net application.

This tool is developed by Google and it is free and open source. The name of the tool is ELMAH.

OVERVIEW
To integrate Elmah tool in your application you need to download the file from the google code site.
To download the file http://code.google.com/p/elmah/.
The zip file contains the source code and bin file. You need to add the Elmah.dll, ElmahErrorLog.dll, VistaDB.NET20.dll, System.Data.SQLite.dll, Elmah.xml file in your applications bin folder.
Add the reference for above dll files.
Error can be logged in many ways.

  1. In memory
  2. Store error in sql server.

In memory store the error in memory. You can view the error at runtime and the error does not gets stores anywhere.

To make sure the data gets store in somewhere, you need to create the table in sql server.
The source code which you download contains the sql server file. Just run the query and the table will be created automatically as shown below.

As shown above it will create ELMAG_error table in your database. IT also creates couple of procedures for inserting data.

Now we need to configure your web.config file to put elmah to work. Add elmah module to your web.config file.

Just specify the data source, user name and password in your connection string and rest of the settings for elmah you need to specify as shown above.

Now we need to make use of elmah for logging the error in the database you have created.

For testing the Elmah error log create a method that will generate the error as shown below.

using System;
using System.Web.UI;
using Elmah;
public partial class ElmahErrorLog : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Division();
}
}

private void Division()
{
try
{
int i = 0;
int j = 2;
int x = j / i;
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(ex);
}
}
}

ErrorSignal.FromCurrentContext().Raise(ex) is used to log error and store it in database.

To check the error, just open the browser and type the URL of your web application followed by elmah.axd. Below is the screenshot.

You can see the screen in the grid format displaying the details of your error.

To view more details related to your error click on the details link next to your error. The details explanation of the error will be displayed as shown below.

This is how you can use elmah error logging for logging errors.

This is just the short description of elmah error logging. For more details for elmah error logging click here.

To download the source code click here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s