Logging Errors with ELMAH (Error Logging Modules And Handlers) in MVC 4

ELMAH is an alternative to HMS (Health Monitoring System) for logging errors which is free and available under open-source which is created and managed by Atif Aziz. HMS was (was...why?) great (with few pains) because it has option to capture all events as well as errors. With HMS developers has to design the UI for viewing the log information but with ELMAH, you just need to hit a URL to view log information.

ELMAH with initial configuration for getting started quickly. ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.

Why I used ‘was’?

Yah. I said ‘was’, because with the release of .NET 4.5 Simple Membership you can’t see aspnet_WebEvent_Events table, now it is getting more difficult to configure HMS in our ASP.NET or ASP.NET MVC projects.

Installing ELMAH

Installing ELMAH is very easy and it works super cool. You can install it in any pre-existing project or newly created project. I am going to create a new ASP.NET MVC 4 Web Application with Internet Application template. Open the project and follow the steps given below.

Step 1: Installing Binaries

Open ‘NuGet Package Manager’ and search for ‘Elmah’ OR use power of ‘Package Manager Console’ and type following.

It will add required library in your project and update the web.config file.

Now you all set, run the project and try navigating to some wrong urls which is not available intentionally and then navigate to elmah.axd on root like http://localhost:4200/elmah.axd and see how Elmah working.

That’s it.

Step 2: Prevent Anonymous

You can prevent anonymous to see this error log information by using highlighted code.

  <location path="elmah.axd" inheritInChildApplications="false">
        <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />

        <allow roles="admin" />
        <deny users="*" /> 

        <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />

You get above codes after installing Elmah automatically.

Step 3: Route Error Logs in SQL Server Database

It is also very simple. Just download the .sql file from here https://code.google.com/p/elmah/downloads/detail?name=ELMAH-1.2-db-SQLServer.sql
and create the database table and also get database connection string from web.config file, better if you created this database table with simple membership database tables.

Now, in web.config seach for <elmah> tag and replace with following codes and also change connectionStringName if needed.

  <security allowRemoteAccess="true" />
  <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="DefaultConnection" />

Now, run the application and created some errors intensionally and open database table to view routed errors.

Super Cool.

Step 4: Route Error Logs in XML File

It is even simpler than above one. Just update <elmah> tag as you updated above, with following.

   <security allowRemoteAccess="true" />
   <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />

And the output is here.

Hope this helps.


  1. Could I add 2 more columns in ELMAH schema and add two extra parameters from C# ? if yes then how to do it. ?


Post a Comment

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples