ELMAH: Open Source .NET Logging Engine

One of the things that helps improve our product and systems is logging exceptions. There are many reasons that why we need to log exceptions and important events somewhere that we can review them later. The most important one is that if you continue to track what you have logged, you will find out where your bugs come from and how to fix them. 

There many different ways to log exception in your code. You can use classes that you have out of the box in System.Diagnostics namespace such as Debug and Trace. You can also create your own logging engine or you can get help from ELMAH.

As it written in its web site "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." Using ELMAH is easy and you can add ELMAH to your project.

Configure ELMAH for an ASP .NET MVC project

1. Open Manage NuGet Packages by right clicking on your project in Solution Explorer

2. Search for ELMAH

3. Select the packages which fits to your project type (my project is a ASP .NET MVC web application so I choose ELMAH.MVC) and click on Install

4. It will install two packages. ELMAH.Core which is the core and ELMAH.MVC whihc is defined for ASP .NET MVC projects.

5. Open your Web.Config file. You will see that a few changes have done in file.

6. Look for a new section called "Elmah" in your web.config file (Here is the place you will set most config for ELMAH)

7. Add a new configuration line like this to configure ELMAH to write the log files in XML format and in a folder names "Logs" in your App_Data folder:

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

8. Notice that you can use SqlServer as your repository, but let start with XML files.

9. You need to configure your http handler which will show the log stream. Find <handlers> node in <system.webServer> section and add the following httpHandler:

<add verb="POST,GET,HEAD" path="Elham.axd" type="Elmah.ErrorLogPageFactory, Elmah" />

10. Now Elmah is up and running and it will catch and log any unhandled exception in your project. You can also use the following code to log handled exception into Elmah repository:

       // Some code that might raise an exception
catch(Exception ex)


11. To see how Elmah is logging your exception just enter Elmah.axd at the end of your site url:


Welcome to my new blog

After a while I had the chance to spend a few hours and bring up a new blog to continue writing about Microsoft .NET and day to day challenges and adventures on it. Here in this blog I will focus on Microsoft .NET and related technologies and share my knowledge and experiences with you. Let’s hope that we can learn from each other.
There was a lot of improvement during last two-there years on Microsoft .NET, especially on my favorite ASP .NET. ASP .NET team has done a great job and now I feel that ASP .NET is going on the road that I should be. I have been working with Microsoft ASP .NET MVC on my recent projects and it's really nice and wonderful framework for developing web sites and web applications. I will share many things that I have learned during my studies recently and get through it with a training series on ASP .NET MVC. Just like before I will also prepare some video training series and post them here in this blog.

Let move on...

About Masoud

.NET Developer & Instructor. CEO and Founder of Honopardaz, Co-Founder of Buyex.ir. Masoud is a Microsoft Certified Professional Developer having 10 years on software development experience with Microsoft technologies. Masoud has held many C# and ASP .NET training courses during his career for lots of individuals and companies.


Month List