Integrating ELMAH with Umbraco

Update: For the latest details on how to integrate ELMAH with Umbraco, please read the article over on the Our Umbraco wiki.

I have a few Umbraco projects that have a lot of custom .NET code, mostly in they are in the form of user-controls and XSLT extensions.  As far as I’m aware Umbraco doesn’t have an extendable mechanism for exception handling and sending out notification emails, (there is the umbraco.BusinessLogic.Log, which writes to the umbracoLog table in the database, but that’s all).

Initially I looked at Tim Gaunt’s Advanced Error Reporting – a great drop-in solution that does exactly what it says on the tin!  Whilst reading the comments on Tim’s blog, Simon Dingley reminded me of the ELMAH project – which has been one of those web-applications that you keep meaning to try out, but never get around to.

What is ELMAH?

ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It 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.

So I decided to see how nicely it plays with Umbraco… the result, it plays very nicely indeed.

If you are interested, here’s how…

  1. Download the latest ELMAH binary release (1.0 Beta 3 at the time of writing [direct link]) from the Google Code project page. (http://code.google.com/p/elmah/)
  2. Extract the files from the ZIP.
  3. Select the DLLs from the “/bin/” folder, for Umbraco you’ll be using the DLL from “/bin/net-2.0/Release/“.  For the benefit of this post, I decided to use the SQLite option to store the error logs in a database. I could easily have used the SQL Server or VistaDB options.
  4. Drop the DLLs into the “/bin/” folder of your Umbraco installation.
  5. Open the web.config of your Umbracoo installation and add the following lines:

Add the following to your <configSections> section:

<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>

Add the following just after the </configSections> section:

<elmah>
	<security allowRemoteAccess="yes" />
	<errorLog type="Elmah.SQLiteErrorLog, Elmah" connectionStringName="ELMAH.SQLite" />
	<errorMail from="no-reply@domain.com" to="webmaster@domain.com" />
</elmah>

Add the following to your <connectionStrings> section, (if you have one, otherwise create one):

<add name="ELMAH.SQLite" connectionString="Data Source=~/data/errors.s3db"/>

In the <httpModules> section, add this:

<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>

… and finally, in the <httpHandlers> section, add this:

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

If you run into any trouble, there is a more detailed guide on Setting Up ELMAH from DotNetSlackers.

By now you should have ELMAH up and running.  Open up your web-browser and go to http://localhost/elmah.axd, (obviously replace “localhost” with whatever your hostname is). You should see the ELMAH Error Log page.  Since this is open to the public, you may want to secure it, see the Securing Error Log Pages article for further details.

The last part is to integrate the ELMAH Error Log page into the Umbraco back-end.  I created a new user-control in the “/usercontrols/” folder called “ELMAH.ascx”, using the following HTML:

<%@ Control Language="C#" %>
<iframe height="98%" width="100%" scrolling="auto" src="elmah.axd" style="margin-top:5px;"></iframe>

Then in the “/config/Dashboard.config” configuration file, I added a new section for the developer area.

<section>
	<areas>
		<area>developer</area>
	</areas>
	<tab caption="Error Logging Modules and Handlers for ASP.NET">
		<control>/usercontrols/ELMAH.ascx</control>
	</tab>
</section>

Now in the Umbraco back-end the developer area looks like this.

ELMAH in Umbraco

I have been very impressed with how well ELMAH functions.  Aside from the essential email notifications, the RSS feeds are a great bonus!

kick it on DotNetKicks.com

23 thoughts on “Integrating ELMAH with Umbraco

  1. Great article Lee,

    Now I should try that out as well, having a bunch of .net controls as well…

    Thanks for sharing.
    /Dirk

  2. This is great,

    Far more easy to integrate as a Log4Net or other framework. And you can actually see the error reports. Great thanks for sharing.

    Richard

  3. Nice one Lee! I tried this after seeing your tweet the other week and it worked great. I had to update the path to include a leading slash, i.e. “/elmah.axd” instead of “elmah.axd”. I used SQL Server too, and to change the compatibility level I needed to use the stored procedure (rather than the command included in the instructions on the Elmah site):
    EXEC sp_dbcmptlevel MY_DATABASE, 80;

    It runs like a dream and you can flag your own error messages to it for debugging – I’ll definitely be using this for every site from now on. Thanks for the tip on the user control for Umbraco’s admin back-end – I hadn’t thought of that bit!

    David

  4. Pingback: Integrating log4net into Umbraco site. « Ismail’s umbraco adventures

  5. Hello Everybody,

    I was just curious if any body has tried integrating ELMAH with MySql, i am trying for the implementation but stuck at the moment.

  6. Hi Rajiv,

    Thanks for your comment, I see that you have been liaising with Atif (the core ELMAH developer) about it – that’s probably your best bet.

    http://groups.google.com/group/elmah/browse_thread/thread/33d2597ad0fd15cd

    A MySQL provider should be straight-forward to implement. To be honest, I’m quite surprised that it has not been done already!

    If you still need help, feel free to contact me via the form on my website:
    http://leekelleher.com/contact/

    Cheers,
    - Lee

  7. Pingback: MySql data-source support for ELMAH « Lee Kelleher’s Weblog

  8. Excellent thanks.
    I made this tweak to get the iframe filling the panel.

    <style>
    .tabpageContent{height:100%;}
    #elmahFrame{border:0;}
    </style>
    <iframe height=”98%” width=”100%” scrolling=”auto” src=”elmah.axd” style=”margin-top:5px;” id=”elmahFrame”></iframe>

  9. Thanks Murray, I’ve been meaning to get around to posting a fix for it in Umbraco v4.

    (I combined your 2 comments into 1 btw)

    I’ve been working on tighter ELMAH integration in my sparse [sic] time – building it as a custom appTree.

  10. Hi Lee,
    did you try securing elmah?
    I have a problem with it, in that umbraco does not seem to login using standard .net authorization ‘roles’.

    Here’s the bit of the web.config that’s not working: (this denies access to everyone)

    <location path=”elmah.axd”>
    <system.web>
    <authorization>
    <allow roles=”Administrators”/>
    <deny users=”*” />
    </authorization>
    </system.web>
    </location>

    Or do I have the role name wrong?
    Cheers.
    Murray.

  11. I recently was trying to get Elmah into one of my projects, but it was just not recording anything at all.

    Turns out, that when you’re running on an asp.net 3.5 config (as opposed to 2.0), you need to add some more keys:

    In system.webServermodules, add:

    And in system.webServerhandlers, add:

  12. Hi David, thanks for your comments.

    Yeah, I wrote this blog post about 18-months ago, when I was still using Umbraco v3 and .NET 2.0.

    I’ll add a message onto the Our Umbraco wiki page – which originally was based on this blog post!

    Cheers,
    - Lee

  13. Pingback: Umbraco, ELMAH, MADAM and authentication | kipusoep's tech blog