Logging Errors With Error Logging Modules And Handlers (ELMAH) in ASP.NET MVC 4

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

ELMAH has an initial configuration for getting started quickly. Error Logging Modules and Handlers (ELMAH) 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 an aspnet_WebEvent_Events table, now it is becoming 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 new project. I will create a new ASP.NET MVC 4 Web Application with Internet Application template. Open the project and use the following procedure.

Step 1: Installing Binaries

Open "NuGet Package Manager" and search for "Elmah" or use the power of "Package Manager Console" and type in the following:

MVC1.png

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

Now you are all set, run the project and try intentionally navigating to some incorrect URLs that are not available and then navigate to ELMAH.axd on the root, like http://localhost:4200/elmah.axd and see ELMAH working. 

MVC2.png

That's it.

Step 2: Prevent Anonymous

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

 

  1. <location path="elmah.axd" inheritInChildApplications="false">  
  2.     <system.web>  
  3.         <httpHandlers>  
  4.             <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" =""/>             
  5.    </httpHandlers>  
  6.         <authorization>  
  7.             <allow roles="admin" =""/>  
  8.             <deny users="*" =""/>  
  9.    </authorization>  
  10.  </system.web>  
  11.     <system.webServer>  
  12.         <handlers>  
  13.             <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" =""/>             
  14.    </handlers>  
  15.  </system.webServer>  
  16.   </location>  
  17.  </configuration>  

 

You get the code above automatically after installing ELMAH.

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 a database connection string from the web.config file. What is even better is if you create this database table with simple membership database tables.

Now, in web.config seach for the <elmah> tag and replace it with the following code and change connectionStringName as needed.

 

  1. <elmah>  
  2.     <security allowRemoteAccess="true" =""/>  
  3.     <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="DefaultConnection" =""/>  
  4. </elmah>  

 

Now, run the application and create some errors intentionally and open the database table to view the routed errors. 

MVC3.png

Super Cool.

Step 4: Route Error Logs in XML File

It is even simpler than the preceding one. Just update the <elmah> tag as above with the following:

 

  1. <elmah>  
  2.      <security allowRemoteAccess="true" =""/>  
  3.      <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" =""/>  
  4. </elmah>  

 

And the output is here.

MVC4.png


Similar Articles