• Increase font size
  • Default font size
  • Decrease font size
Home Development How to add simple Error logging to an ASP.NET application

How to add simple Error logging to an ASP.NET application

E-mail Print PDF

When building an enterprise application, many would recommend using Microsoft's Enterprise Library for error logging.  If you are already using Enterprise Library in your application, then you may want to go that route.  For applications where simple error/exception logging is needed, I would personally recommend using ELMAH (Error Logging Modules and Handlers for ASP.NET).  Adding ELMAH to your application is very simple to do, and gains you the following features:

  • 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, including colored stack traces.
  • In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with customErrors 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.

Here I will walk you through quickly adding ELMAH to your application.  For this example, I will be logging errors to a Microsoft SQL database table, but the tool is configurable to several different datasources (MS SQL, MS SQL CE, MS Access, Oracle, SQLite, XML Files, MySQL, PostgreSQL).

  1. First, download the Core Binary and MS SQL Server DDL Script from the project website.
  2. Now you will need to execute the DDL script against your application's database (or a different one if you desire).  This script will create the logging table and a few stored procedures.
  3. Next, extract the Core Binary .zip file and drop the Elmah.dll file into your website's Bin directory.
  4. Now you will add a few lines to your application's web.config (see images below).  This configures ELMAH to log application execeptions.
    web config entries
    elmah web config entries
  5. In this case, I didn't want all site users to be able to view the error log.  So, I added the following to a web.config file in the /Admin directory of my application.  If you wanted all users to access the error log, you would put this in your root web.config file.
    elmah web config entries

Now to view your errors you would view the page http://[websitename]/elmah.axd (even though this file does not physically exist).  In this case, I wanted the errors viewable only to admins, so the URL would be http://[websitename]/admin/elmah.axd.  Here is a snapshot of the error page you will see:

elmah error page example

There you have it.  Simple Exception logging for your ASP.NET site using ELMAH. 




Recommended Web Hosting

Free Stuff!

Featured FREE Resource:

Featured FREE Resource: