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:



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. 



That's it.


Step 2: Prevent Anonymous


You can prevent anonymous to see this error log information by using the 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 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.



    <security allowRemoteAccess="true" =""/>

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



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



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:


     <security allowRemoteAccess="true" =""/>

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



And the output is here.



Up Next
    Ebook Download
    View all
    View all