Skip to main content

Quick Elmah setup

Elmah is great. Its an error logging utility that you can drop into a .net app without changing any code. You just drop the DLL in the bin folder, and make a few web.config changes. OK, these days you can achieve the same thing with ASP.NET Health Monitoring, but Elmah is nice and easy, and the web pages it provides to inspect the error log are cool.

There are loads of different ways of configuring Elmah (you can get it email errors to you, for example) and the 'demo' that comes with the download shows most of the options. But in most cases you'll want bog-standard logging to a local db, so these notes cover doing that.

1. Download Elmah

Download from here.

2. Drop DLLs into Bin folder

Unpack the download and look for the appropriate bin folder, usually you'll want the Release version of .NET 2.0 or .NET 3.5. From that bin folder, copy the Elmah.dll and the SQLLite.dll to the Bin folder for your web app.

E.g, these two files:
ELMAH-1.1-bin\bin\net-2.0\Release\Elmah.dll
ELMAH-1.1-bin\bin\net-2.0\Release\System.Data.SQLite.dll


3. Add things to your web.config

Copy the bold sections below into the right places in your web.config:

<?xml version="1.0"?>
<configuration>
  <configSections>
    ... 
    <sectionGroup name="elmah">
      <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
      <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/>
    </sectionGroup>  </configSections>
  ...
  <elmah>
    <errorLog type="Elmah.SQLiteErrorLog, Elmah" connectionStringName="ELMAH.SQLite" />
    <security allowRemoteAccess="0" />
  </elmah>
  <connectionStrings>
    <add name="ELMAH.SQLite" connectionString="Data Source=|DataDirectory|errors.s3db"/>
    ...
  </connectionStrings>
  <system.web>
    ...
    <httpHandlers>
      ... 
      <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah"/>
    </httpHandlers>
    <httpModules>
      ...
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    </httpModules>
    ...
  </system.web>
  <system.webServer>
    <modules>
      ..
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
    </modules>
    <handlers>
      ...
      <add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD"
        type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
    </handlers>
  </system.webServer>
  ...
</configuration>
4. Off it goes!

Thats it, really. Elmah will now catch and log all unhandled Exceptions.

If you now run your web app, and on the web server browse to:
http://localhost/YourAppName/Elmah.axd

... you will see the Elmah error log.

Note that the config settings given above ensure that viewing the error log is only accessible from the web server itself.

If you want to manually log errors, for example Exceptions that you have caught in a try..catch block, then you can use code like this:
catch (Exception e)
{
  Elmah.ErrorSignal.FromCurrentContext().Raise(e);
  ...
}
(If you are using an ASP.NET Web Application Project, you'll also need to add a reference to Elmah.dll to be able to access Elmah in code)

Comments

Popular posts from this blog

SSRS multi-value parameters with less fail

SSRS supports multi-value parameters, which is nice, but there are a few issues with them. This is how I deal with them. Two of the problems with SSRS multi-value parameters are: You have to jump through a few hoops to get them to work with stored procedures The (Select All) option, as shown above The reason the (Select All) option is a problem is that it is a really inelegant way of saying 'this parameter does not matter to me'. If you have a list with hundreds of values, passing all of them as a default option just seems wrong. Also, if your report shows the user which items they selected, printing the whole list when they choose (Select All) is excessive. So in this post I'm going to show my particular way of: Jumping through the hoops to get Multi-Value params in a stored procedure Adding a single '--All--' value that the report interprets as meaning all the options. Getting Multi-Value params to work with Stored Procedures This is

Copying data to Salesforce Sandboxes using TalenD

A common problem with Salesforce Developer Sandboxes is that they are blank. Really you're going to want some data in there, so there are various strategies for copying data from your live instance to the Sandbox. There are some paid-for solutions - SFXOrgData , Salesforce Partial Data Sandboxes - but if you've got a decent ETL tool you can build your own. There are a bunch of free ETL tools for Salesforce: JitterBit Data Loader is good for quick ad-hoc tasks but the free version makes it difficult to manage specific ETL projects or share projects with other users Pentaho Community Edition - an open source edition of the enterprise version Apatar was a free open source Salesforce ETL which still works but development seems to have stopped since 2011 TalenD Open Studio is an open source ETL tool For the task of copying data from live to a Sandbox, either Pentaho or TalenD Open Studio could be used, depending on preference. Here's a good comparison of the dif

Bug Hunter in Space

In 1987, Acorn launched the Archimedes home computer. At the time, it was the fastest desktop computer in the world, and at the time, I was fortunate enough to have one to experiment with. The Archimedes was great, but it never really took off commercially. However, it was built around the ARM processor, which Acorn had designed itself when it could not find any existing processors suitable for its 32-bit ambitions. The ARM processor was a masterpiece of simple and intuitive design, and its still around today, with most of the instruction set pretty much unchanged. In fact, you've probably got one in your pocket right now. Its design makes it process very efficiently on low energy intake, and hence it is estimated that about 98% of all mobile phones contain an ARM chip. Over 10 billion ARM chips have been shipped, and they outnumber Intel's long running x86 series of chips by a factor of about 5 to 10. I had learned programming on the BBC Model B , and when we got the A