Lee Kelleher

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


  • http://kenny.no/ Kenneth Solberg

    Nice article. Thanks for sharing!

  • http://www.netaddicts.be Dirk

    Great article Lee,

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

    Thanks for sharing.
    /Dirk

  • http://www.richardsoeteman.net/ Richard

    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

  • http://www.nibble.be Tim Geyssens

    Works great, small issue when installing on x64 server. Needs other SQLLite assembly

  • http://prolificnotion.co.uk Simon Dingley

    Great stuff Lee, worked a treat for me.

  • http://web-garden.co.uk David Conlisk

    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

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

  • ismailmayat

    Lee,

    Just found this about sending elmah logs via Gmail http://scottonwriting.net/sowblog/posts/13845.aspx could be of interest.

    Regards

    Ismail

  • Mario Allegro

    Superb!! It works like a charm.

  • Rajiv

    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.

  • http://leekelleher.com/ Lee Kelleher

    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

  • http://www.cpalm.dk Christian Palm

    Thanks for sharing, works perfect with umbraco (using the MS SQL edition)

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

  • http://www.terabyte.co.nz Murray

    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>

  • http://leekelleher.com/ Lee Kelleher

    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.

  • http://www.terabyte.co.nz Murray

    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.

  • http://leekelleher.com/ Lee Kelleher

    Hi Murray, I haven’t tried to set ELMAH up for remote access for a specific user group/role.

    You could try the ELMAH support group forum? See if they have any ideas?
    http://groups.google.com/group/elmah

  • http://cultiv.nl Sebastiaan Janssen

    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:

  • http://cultiv.nl Sebastiaan Janssen
  • http://www.electricbiro.co.uk Rob Stevenson-Leggett

    Lee,

    Just come across this again and realised I used this in a project I (and later you too!) worked on. Thanks for the info! :-)

    Rob

  • http://davidsiew.wordpress.com davidsiew

    Hey Lee,

    Thanks for the great post. However, I should say that it would be better to follow the instructions at http://our.umbraco.org/wiki/how-tos/use-elmah-with-umbraco because there is some code missing in your blog for some reason. There’s also a section that needs to be added when using the .NET 3.5 version. I can also confirm that the instructions work for Umbraco 4.5.1.

  • http://leekelleher.com/ Lee Kelleher

    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

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