Introduction

Error Logging Modules & Handlers (ELMAH) is a tool for debugging mainly ASP.NET applications as suggested by the wiki. Since we all know there is always a chance of getting exceptions when working on any applications. Handling them is a real challenge for many developers. But if we have a tool then it can provide the details of the exceptions or errors with the stack trace as well. Apart from showing the users the Yellow screen of death, we show the users a customized error page and for the developers if an exception occurs then instead of a try/catch block for every method, ELMAH provides them the details of the exceptions with the entire stack trace and a solution based on the error code. For more information on error codes and redirection please follow one of my other articles Custom Errors. Let's get into more details of how this beautiful tool works.

What we get

You would be wondering what this ELMAH gives us after adding this into the application. Straight from the Hanselmen's blog, ELMAH would provide us the following without changing a single peice of code. Since ELMAH includes modules and handlers, it greatly supports HttpModules and Http Handlers.

Suppose the preceding is the case and the user using the application lands on the preceding redirect path URL pages, whereas the error and exception are managed by ELMAH and mailed or logged into the database for future reference for the developers.

Installation for an existing MVC project

Step 1

Go to references and right-click on the Manage Nuget Packages.

Manage Nuget Packages

Step 2

Search online for Elmah.MVC.

Elmah

Step 3

After the successful installation, you can check the package.config for the version of ELMAH installed as shown below:

config

Step 4

You need to ensure the following web.config configurations as shown in the images below:

  1. <sectionGroup name="elmah">
  2. <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
  3. <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
  4. <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
  5. <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
  6. </sectionGroup>
  1. <system.web>
  2. <authentication mode="None" />
  3. <compilation debug="true" targetFramework="4.5.1" />
  4. <httpRuntime targetFramework="4.5.1" />
  5. <httpModules>
  6. <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
  7. </httpModules>
  8. <httpHandlers>
  9. <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
  10. </httpHandlers>
  11. </system.web>
  1. <system.webServer>
  2. <modules>
  3. <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
  4. </modules>
  5. <handlers>
  6. <add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
  7. </handlers>
  8. </system.webServer>
  1. <elmah>
  2. <security allowRemoteAccess="yes" />// To allow remote access
  3. </elmah>
Now everything is set for developers to check for the internal exceptions. But how to access the ELMAH interface? It is simple, just use the path URL succeding with /elmah.axd then when that is done you see the list of exceptions/errors that have occured. The interface would look as in the following image:

errors

Integration of ELMAH into Gmail

Yes, you heard it right. As the heading says, we can include email settings into ELMAH and send the entire stack trace and exception details as mail to multiple users using a subject too. The following explains the integration and setup for the mail settings. Here I use the Gmail settings. The best part here is the change is only done at the web.config level. The following are the proper configurations.

Thus, we have discussed everything related to ELMAH. In the future article/blog I will be explaining how to store into tables using connection strings. The interaction of SQL Server of ELMAH.

References

Scott Hanselman Blog Wiki.