How to add simple logging to your Web App using Elmah
Logging is a crucial part of any application. Logging can be used to track events, identify code issues, security holes, etc. In this blog post, I’ll describe how to add logging to your ASP.NET web application using an open source logging framework called Elmah.
Elmah has been around for a while, it became very popular for its pluggable framework. Elmah can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.
Another common open source logging framework for .NET is a port of the excellent Apache log4j framework to the Microsoft .NET runtime, log4net. However, in this blog, I’ll focus on how to enable logging for your ASP.NET web application using Elmah.
Install Elmah
Get the latest Elmah version by typing the following into your Package Manager Console.
PM> Install-Package elmah.sqlserver
In this example, I am using the Elmah SQL package that comes with the files and configuration needed to log errors in a SQL table. After running the command above, you’ll see a new directory, App_Readme with two files in it.
Add Elmah Table
Open the SQL script Elmah.SqlServer.sql in SSMS and execute it against your database, this will create one table and three stored procedures. If you have SQL Server Data Tools installed in Visual Studio, you can also just double-click the Elmah.SqlServer.sql file and run it inside Visual Studio.
- ELMAH_Error
- ELMAH_GetErrorXml
- ELMAH_GetErrorsXml
- ELMAH_LogError
Edit your web.config with the correct settings in the elmah <connectionString> to connect to your database
Configure Elmah
To make my web application work with Elmah, I had to make the following changes to the Web.config file.
Make sure you follow the steps below, Elmah’s site instructions do not mention adding the Elmah section group to the web.config file, if you don’t do this Elmah won’t work, and you’ll get an error when trying to launch it.
- Remove the sample connection string added by Elmah and just use your existing connection string.
<add name="elmah-sqlserver" connectionString="Data Source=****;User ID=****;Password=****;Initial Catalog=****;" providerName="System.Data.SqlClient" />
- Find the following in your Web.config and update the connectionStringName with the name of your existing connection string.
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="DefaultConnection" />
- Add the following to the Configuration section of your web.config.
<configSections> <sectionGroup name="elmah"> <section name="security" type="Elmah.SecuritySectionHandler, Elmah"/> <section name="errorLog" type="Elmah.ErrorLogSectionHandler, Elmah"/> <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" /> <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" /> </sectionGroup> </configSections>
Using Elmah
Once you have followed the steps above you’ll be set. Just browse to your web app and at the end type /elmah.axd to view your log. You can also query your Elmah error log table to see the data Elmah collects.
To view the error log page remotely, you’ll need to change the following setting in your web.config file to “1”.
<security allowRemoteAccess="1" />
You’ll want to secure access to Elmah if you do this, otherwise anyone will have access to see your error logs. I will write a follow-up post about how to properly secure and restrict access to Elmah logs. Stay tuned.
You can also add your own exceptions or non-exceptions messages to Elmah. This is useful to log events or other useful information. To do this, just use the syntax below.
Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Anything you want to log goes here."));