- ASP.net, MVC3

Catching ASP.NET MVC Errors using ELMAH

ELMAH (Error Logging Modules and Handlers) is a great application-wide error logging tool.

ELMAH Features:

  • Logging of unhandled exceptions.
  • A web page for viewing the logged exceptions including stack traces.
  • An e-mail notification whenever an error occurs including the error details.
  • An RSS feed that includes the last 15 errors from the log.

Installing ELMAH:

The easiest way to install ELMAH is using Nuget. If you are using ELMAH in an MVC project, I suggest installing ELMAH.MVC which will install ELMAH and a custom MVC Area for accessing the logs at “/Admin/ELMAH” directory. It will also setup the basic configurations in your web.config file.

ELMAH.MVC Nuget Package

If you don’t want the added ELMAH MVC Area or if you are installing ELMAH on an ASP.net WebForms project, just install the regular ELMAH nuget package.

ELMAH Nuget Package

Configuring ELMAH:

You will need to open up your web.config file to make configuration changes.

Customer Error Pages:

To fully benefit from ELMAH, you should set your Custom Errors configuration to “On” or “RemoteOnly”. The default custom errors settings is “RemoteOnly”.

Storing errors:

By default, ELMAH stores up to 15 errors in memory. It can be configured to hold up to 500 in memory.
Memory configuration:

ELMAH can also store errors in XML files with each error having it’s own XML file. The log path must be a sub folder of the application and have write permissions.
XML configuration:

Along with memory and XML file storage, ELMAH can store errors in SQLite and SQL Server
SQLite configuration:

SQL Server configuration:

Error Emails:

You can have ELMAH send you an email with a detailed report for each error by adding the following to the web.config:

Securing ELMAH:

Elmah is set to deny remote access to the error log page but you can enable it and use ASP.net security measures by either securing the controller or the elmah.axd path.

Filtering Errors:

You can programmatically filter errors in your Global.asax file. You can filter errors from being emailed to you or logged. Here is a quick example of how to dismiss URL errors in the error mail. You just need to include the Elmah namespace and add the following function to Global.asax:

About Joey Iodice

I'm a software developer focusing on ASP.net, HTML5, jQuery, & Javascript.
Read All Posts By Joey Iodice

Leave a Reply

Your email address will not be published. Required fields are marked *