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:

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. <system.web>
  11. <httpModules>
  12. <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
  13. <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
  14. <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
  15. </httpModules>
  16. </system.web>
  17. <system.webServer>
  18. <modules>
  19. <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
  20. <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
  21. <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
  22. </modules>
  23. </system.webServer>
  24. <elmah>
  25. <security allowRemoteAccess="false" />
  26. </elmah>
  27. <location path="elmah.axd" inheritInChildApplications="false">
  28. <system.web>
  29. <httpHandlers>
  30. <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
  31. </httpHandlers>
  32. <!--
  33. <authorization>
  34. <allow roles="admin" />
  35. <deny users="*" />
  36. </authorization>
  37. -->
  38. </system.web>
  39. <system.webServer>
  40. <handlers>
  41. <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
  42. </handlers>
  43. </system.webServer>
  44. </location>
  45. </configuration>
  46. If you want to get Exception through mail then add the following line of code.
  47. <errorMail from="[email protected]" to="[email protected]" Subject="Application Error" async="true" smtpPort="0" useSsl="true"></errorMail>
  48. Enter the above line of code must be added within the
  49. <elmah> tag Now we have to config the email account. By using the below excel tags
  50. <mailSettings>
  51. <smtpdeliveryMethod="Network">
  52. <network host="smtp.gmail.com" port="587" userName="Your mail address " password="your password " />
  53. </smtp>
  54. </mailSettings>
  55. </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: