The ability to efficiently log and analyse errors is an integral part of any system. A way to quickly view information about an error is critical especially when the application is in production.
I’ve used custom error logging mechanisms and logging frameworks such as Log4net and Nlog in the past, but there is one framework that has become a standard in all my ASP.Net projects.
ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable.
- Logging of nearly all unhandled exceptions.
- A web page to remotely view the entire log of recoded exceptions.
- A web page to remotely view the full details of any one logged exception.
- In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with custom Errors mode turned off.
- An e-mail notification of each error at the time it occurs.
- An RSS feed of the last 15 errors from the log.
- A number of backing storage implementations for the log, including in-memory, Microsoft SQL Server and several contributed by the community.
It comes with a nice and simple web page to view error logs and include a detailed view where information such as request, response, stack traces and just about all the data you would want to see when tracing and investigating errors.
The most common web portal project setup that I’ve work with is ASP.Net with SQL Server as the database back-end. Here’s a quick step-by-step instruction on how you can implement ELMAH in an MVC app using SQL Server.
1. Install Elmah.MVC nugget in your MVC project.
2. After installation, web.config entries will be generated. Since we’re using SQL Server to store our logs you need to modify the connection string. Note that the highlighted connections string names for ELMAH must match.
3. ELMAH required tables and stored procedures to be created in your SQL database. You can download the DDL script here:
Download and run the SQL DDL script on your database.
4. In your FilterConfig.cs, since ELMAH will handle all error logging, comment that default error handler attribute.
And you’re done! Compile and run your project. You can view the ELMAH web page using the address http://<yoursite>/Elmah.axd. Here’s a screenshot of the ELMAH log page.
ELMAH Log List.
Logging handled errors quietly:
One common feature that you may want to implement in your project is logging critical events, or sometimes, you just want to “quietly” log an exception without alerting the user. ELMAH has the ability to raise an “ErrorSignal” with the syntax below.
Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception(“Something error has occurred.”));
In the sample snippet below, I’m sending a Notification email after a successful transaction. The main operation is already processed successfully and Notification is just a secondary non-critical operation. If any error occurs attempting to send the email, then we log the error quietly using ELMAH. The information logged will consist of the Current Context and Exception information.