Error Handling is the one of the most important thing which we used to track errors.

There are different ways of handling errors which developers preferred to log errors. Some developers write their own code to log errors in database, XML files or either text files.

There are also third party tools available to log errors. In this article I will show you how to log errors in the database using Google Elmah error handling in MVC application.


To integrate Elmah tool in your application you need to download the file from the Google code site.

To download the file click on the following link: –

Or you can install through the Nuget package manager. You can install the Nuget package manager through the visual studio Library Package Manager. For more information visit

I will be installing the Elmah using the Nuget package manager.

I will be demonstrating the use of Elmah in Visual Studio 2012 using MVC 4 application.

Open the visual studio 2012 and select New Project from the start menu.

This will open the New Project dialog box as shown below.


Select MVC 4 web Application from the template and provide the name of the project.

This will open the MVC 4 project template as shown below.


Select the Internet application and click on the OK button to launch the MVC 4 project.

Now, we will install the Elmah through the Nuget package manager.

Select tools from the menu and point to Library Package Manager. Select Manage Nuget Packages for Solution as shown below.


This will open the Package Manager Console. Now install the Elmah using the below command.

Install-Package Elmah

This will install the Elmah package in to the application as shown below.


It will automatically add all the details in to the Web.Config file.

A couple of modification is required to make in the Web.config files to make Error logging work.

You need to add handlers and make modification in the Elmah section as shown below.

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 below.


Now you need to create the database for logging the errors.

To download the Elmah script visit the link

Once done with the above configuration we will write a code to generate the error.

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.

private void Division()
int i = 0;
int j = 2;
int x = j / i;
catch (Exception ex)

Call this method in the Index Action Result.

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.


Now access the URL for example. http://localhost:6061/elmah.axd

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.


For more information Elmah Error logging refer to the below link.

To download the demo project, click Here.


One comment

  1. If ELMAH is installed into the project and web.config, you don’t need to manually catch exceptions and log them to ELMAH. The ELMAH handlers and modules will do this for you. If the purpose of using try-catch is to suppress the error to the client, the code is just fine 🙂

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s