By Jennifer Marsh
Jennifer Marsh is a software developer, programmer and technology writer and occasionally blogs for Rackspace Hosting.
Error handling traps errors on a website, logs the issue to a database or file, and presents the user with a friendly message. ELMAH (Error Logging Modules and Handlers) is a free, ASP.NET logging system that helps website owners handle errors and logs them to an XML file or database table. The advantage to ELMAH is that none of the errors are seen by the user. Even search engine errors are absorbed, logged and handled eloquently without affecting performance of the website or revenue.
Setting Up ELMAH in the .NET Project
ELMAH’s settings are placed in the web.config for the .NET project. Every cloud application for a website has a web.config. The lines of code needed in the web.config are on the ELMAH Wiki. In the “connectionString” section, the database name, drivers and username and password are entered. Typically, the business uses a separate table from the rest of the application to keep errors separate from the production database.
Finally, to log to XML files in a specified directory, add the directory on the web server in the “logPath” value. This value is the local path on the web server in which the ELMAH files are stored.
Opening the ELMAH XML Log Files
To see a list of errors, open the “elmah.axd” file located at the root of the domain. ELMAH gives a list of detailed errors including the host name, the server response code, the description of the error from the stack trace, the user (if a customer was logged-in), and the date and time. The AXD file is the easiest way to view the list of XML errors the server stores.
To view the errors in the database, the “elmah errors” table is created when the ELMAH database is set up. The advantage to viewing the errors in the database is that the developer can filter out records and find errors that happened at specific times or in specific pages. This can help narrow down any performance issues or configurations that are throwing unhandled exceptions. Most notably is that that host column will identify if the errors are happening for users or for search engines.
Another advantage to ELMAH is that there is little configuration, and it handles all errors the developer forgot to catch in the application. The standard “try/catch” code catches errors and displays a friendly error to the user. However, if the developer forgets this block of code, the application shows a blatant error to the visitor, which increases bounce rate. In some cases, the developer might want to purposely insert an incident into the ELMAH log. To do this, add the following code to the try/catch block:
This type of error handling helps the application run much more smoothly, and it helps developers find the bugs in larger applications where bugs might not be evident during testing.