Ask your technical questions on forums or here :
ASP.NET or MVC | C# | Windows Phone
Microsoft Technology Journals by Abhimanyu K Vatsa
HOME ABOUT RAZOR BOOK SPEAKING MVC ASP.NET JQUERY VIDEOS EBOOK ARCHIVE

3 Mar 2013

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">
    <system.web>
      <httpHandlers>
        <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
      </httpHandlers>

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

    </system.web>
    <system.webServer>
      <handlers>
        <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
      </handlers>
    </system.webServer>
  </location>
</configuration>

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.

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

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.

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

And the output is here.


Hope this helps.

Comment using Google Services (1 comment):

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

    ReplyDelete