Installing ELMAH for simple error logging

This is easily the simplest error logging method I have ever used - it is fantastic!

Error logging is one of the necessary evils of programming. No matter how good you think your program is, it is going to have errors (even with my code, which is flawless... ha). If you are not logging errors some way, you should be, right now!

ELMAH is an error logging framework for Asp.Net and it literally could not be easier and more convenient. You can go from reading this post to having ELMAH logging your errors, publishing RSS feeds and all sorts of other crap in a matter of 5 minutes. This tutorial deals with SQL Server, but you can also store your errors in many other ways such as XML, memory, Oracles, etc. - most of them are well documented... did I say this tool is awesome yet?

get what you need

Go to the download page and download whatever version you want; I am using 1.0 BETA 3. Then simply copy the (the proper one based on your .Net version) Elmah.dll to your bin folder; that's really all you need to do. Alternatively, if you you double-click on demo.cmd it will lauch a demo with Cassinni - I was very impressed with this little added functionality!

make your error logging table is SQL

Pretty much a no brainer here: there is a file called SQLServer.sql in the db folder you just downloaded; run it on the database you want to store the errors in. I used an existing DB so I could just reuse permissions and connectionStrings.

set up your web.config

Just a few changes to your web.config, and you will be up and running. First, add this into your <configSections>:

<sectionGroup name="elmah">
  <section name="security" requirePermission="false"
    type="Elmah.SecuritySectionHandler, Elmah" />
  <section name="errorLog" requirePermission="false"
    type="Elmah.ErrorLogSectionHandler, Elmah" />
  <section name="errorMail" requirePermission="false"
    type="Elmah.ErrorMailSectionHandler, Elmah" />
  <section name="errorFilter" requirePermission="false"
    type="Elmah.ErrorFilterSectionHandler, Elmah" />
</sectionGroup


Next, add this new section directly below your <configSections>:

<elmah>
  <security allowRemoteAccess="yes" />
  <errorLog type="Elmah.SqlErrorLog, Elmah"
    connectionStringName="some_con_string" />
</elmah>


Be sure to set this up with the proper connectionString. Note that I allow remote access, be sure to change this if you don't want to. Now go into the <httpHandlers> section and add this:

<add verb="POST,GET,HEAD" path="elmah.axd"
  type="Elmah.ErrorLogPageFactory, Elmah" />


And the last thing you need to do is add this to your <httpModules> section:

<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />


you are now logging errors

No need to do anything else. Now just navigate to ~/elmah.axd and you will see your error log. All unhandled errors will be logged, no matter what your customErrors is set to! Also, if you want to log handled errors you just grab them with this:
ErrorSignal.FromCurrentContext().Raise(ex);

Where ex is your Exception.

I can't give enough praise for this tool, it is just awesome! Much better than my crappy home-brewed error logging and so much more easy to use.

Comments (1) -

Pingbacks and trackbacks (4)+

Add comment

Loading