Error Logging Modules And Handler (ELMAH) In ASP.NET

In this article you will learn about Error Logging Modules and Handler (ELMAH) in ASP.NET.

ELMAH stands for Error Logging Modules and Handler, it provide error logging facilities to the application. ELMAH is one of the easiest error logging package. It can be added automatically into a running ASP.NET Web Application.

Once ELMAH is installed and configured properly, we can easily use this without changing the single line of code.

ELMAH has the following three main categories:

  • HTTP Module: HTTP module is a class that define event handler for the HTTP Application. The ELMAH includes multiple HTTP modules. In that, the following are the three of them mostly used.

    o ErrorLogModule- Logs the unhandled errors in the Log.
    o ErrorMailModule- Sends Errors of unhandled exception through mail.
    o ErrorFilterModule- Filter to determine what exceptions are logged.

  • HTTP Handler: It is a class that is responsible for generating markup for particular type of request. The HTTP Handler creates the error details as web page, a RSS Feed or a CSV file.

  • Error Log Source: This class allow to log the error into the database.

Create a new MVC Project (for testing the ELMAH).

mvc

Then go to the Nuget Package Manager and install ELMAH,

nuget

After installing the ELMAH it can alter config file automatically. It makes easy to use the ELMAH. If not configured, enter the following in the config file.

  1. <configuration>  
  2.     <configSections>  
  3.         <sectionGroup name="elmah">  
  4.             <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />  
  5.             <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />  
  6.             <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />  
  7.             <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />  
  8.         </sectionGroup>  
  9.     </configSections>  
  10.   
  11.     <system.web>  
  12.         <httpModules>  
  13.             <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />  
  14.             <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />  
  15.             <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />  
  16.         </httpModules>  
  17.     </system.web>  
  18.   
  19.     <system.webServer>  
  20.         <modules>  
  21.             <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />  
  22.             <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />  
  23.             <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />  
  24.         </modules>  
  25.     </system.webServer>  
  26.     <elmah>  
  27.         <security allowRemoteAccess="false" />  
  28.     </elmah>  
  29.     <location path="elmah.axd" inheritInChildApplications="false">  
  30.         <system.web>  
  31.             <httpHandlers>  
  32.                 <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />  
  33.             </httpHandlers>  
  34.             <!--   
  35. <authorization>  
  36. <allow roles="admin" />  
  37. <deny users="*" />  
  38. </authorization>  
  39. -->  
  40.         </system.web>  
  41.         <system.webServer>  
  42.             <handlers>  
  43.                 <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />  
  44.             </handlers>  
  45.         </system.webServer>  
  46.     </location>  
  47. </configuration>  
  48. If you want to get Exception through mail then add the following line of code.  
  49. <errorMail from="mohammedibrahim.lj@gmail.com" to="mohammedibrahim.lj@gmail.com" Subject="Application Error" async="true" smtpPort="0" useSsl="true"></errorMail>  
  50. Enter the above line of code must be added within the  
  51. <elmah> tag Now we have to config the email account. By using the below excel tags  
  52.     <mailSettings>  
  53.         <smtpdeliveryMethod="Network">  
  54.             <network host="smtp.gmail.com" port="587" userName="Your mail address " password="your password " />  
  55.             </smtp>  
  56.     </mailSettings>  
  57.     </system.net>  
In my project I have created an error so that I will know whether the ELMAH package is running or not.

While running the code I got the following error,

error

The exception can be seen in many ways,

We can use the url, to know the list of error occurred. Here's the screenshot:

error

We have to add /elmah.axd at the end of the url.

When the exception is fired it will automatically send a mail to the given mail address like the following message.

message

So, we found the exception using ELMAH.
 
Read more articles on ASP.NET: