What is ELMAH?

ELMAH provide way for logging Error at runtime in a Local/Production environment. ELMAH is a open source error logging library/dll that includes features like error filtering and able to view the error logs by a web page (localhost:50287/elmah.axd). It would very difficult to track/debug the error after code has been moved to production environment. To avoid this type of difficulties we are using ELMAH for logging.

Note : The main purpose of ELMAH it to log all the unhandled exceptions in our application.

Implementing ELMAH in our Application :

STEP 1: Installing ELMAH

Install ELMAH using NuGet, go to Tools -> Library Package Manager -> Package Manager console and type “Install-Package elmah” as shown in below.

STEP 2:

After successfull installation, elmah will add required library in our application and update the web.config file. Verify whether below changes has been updated in the webconfig after the successfull installation.

<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <sectionGroup name="elmah"> <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" /> <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" /> <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" /> <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" /> </sectionGroup> </configSections> <elmah> //Make chage to YES, if we need to access it in remote <security allowRemoteAccess="No" /> </elmah> <system.web> <httpModules> <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" /> <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" /> <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" /> </httpModules> <httpHandlers> <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" /> </httpHandlers> </system.web> <system.webServer> <handlers> <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" /> </handlers> </system.webServer> </configuration> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <? xml version = "1.0" encoding = "utf-8" ?> < configuration > < configSections > < sectionGroup name = "elmah" > < section name = "security" requirePermission = "false" type = "Elmah.SecuritySectionHandler, Elmah" / > < section name = "errorLog" requirePermission = "false" type = "Elmah.ErrorLogSectionHandler, Elmah" / > < section name = "errorMail" requirePermission = "false" type = "Elmah.ErrorMailSectionHandler, Elmah" / > < section name = "errorFilter" requirePermission = "false" type = "Elmah.ErrorFilterSectionHandler, Elmah" / > < / sectionGroup > < / configSections > < elmah > //Make chage to YES, if we need to access it in remote < security allowRemoteAccess = "No" / > < / elmah > < system . web > < httpModules > < add name = "ErrorLog" type = "Elmah.ErrorLogModule, Elmah" / > < add name = "ErrorMail" type = "Elmah.ErrorMailModule, Elmah" / > < add name = "ErrorFilter" type = "Elmah.ErrorFilterModule, Elmah" / > < / httpModules > < httpHandlers > < add verb = "POST,GET,HEAD" path = "elmah.axd" type = "Elmah.ErrorLogPageFactory, Elmah" / > < / httpHandlers > < / system . web > < system . webServer > < handlers > < add name = "ELMAH" verb = "POST,GET,HEAD" path = "elmah.axd" type = "Elmah.ErrorLogPageFactory, Elmah" preCondition = "integratedMode" / > < / handlers > < / system . webServer > < / configuration >

STEP 3:

Run the application and try to access contoller/action which is not present in our application for tracking the error.

After that include elmah.axd at the end of the url to show the errors which has been caught from starting as shown below.

NOTE : If you not able to access the elmah.axd page then include the below line in Global.asax

RouteTable.Routes.Ignore(“{resource}.axd/{*pathInfo}”);

We will discuss more about ELMAH in upcoming posts.

Keep cool coding…