My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
MVC  
Using ELMAH with ASP.NET MVC
Updated Dec 25, 2011 by azizatif

Note: Since ASP.NET MVC 1.0 ignores .axd rules by default this Guide has become mostly irrelevant. For detailed installation instructions look at the DotNetSlackers Article.

Setting up ELMAH on MVC is really simple since most of the work is done in web.config that is more or less shared between MVC and Webforms. There are already excellent (and more detailed) articles in this Wiki on how to configure and fine tune ELMAH. This Article is intended to provide a quick and simple tutorial on the most important steps to get ELMAH up and running on MVC.

(For advanced users seeking MVC-specific information, skip ahead to Step 4)

Step 1: Referencing the assemblies

First, grab the latest binary release of elmah from the project’s page and extract the bin folder.

ELMAH requires no GAC installation, so you can drop the contents of the bin to any location of your liking (although I usually recommend using a lib folder to store external dependencies) and reference the Elmah.dll from within your app.

Step 2: Edit your web.config to call ELMAH

First add the following code to your <configSections> to make ELMAH read it’s configuration from web.config:

<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 go to your <httpHandlers> section and add the elmah file handler:

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

This will reroute all requests to a file called elmah.axd to the ELMAH error-overview page. So when you want to look at the list of errors you’ll access http://server/elmah.axd. The name doesn’t matter, feel free to rename it, but be aware that the extension has to be mapped to the ASP.NET pipeline inside IIS (so naming it .html wouldn’t work if not configured correctly).

At last add the ELMAH logging module to your <httpModules> section:

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

Step 3: Configure ELMAH

I’d suggest you read the wiki articles on how to configure ELMAH correctly, but for getting it up and running quickly we'll just log all errors to XML files by simply adding this code to the web.config:

<elmah>
  <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />
</elmah>

This instructs ELMAH to create XML files in your App_Data directory (so make sure the ASP.NET process has sufficient access rights to that folder) and generate output like this:

Step 4: Configure routing

Up until now we were 100% true to the normal configuration routine for normal ASP.NET applications, there is only one slight adjustment to making it work in MVC.

You need to allow the requests to the ELMAH front-end (elmah.axd in this example) to pass through the MVC routing logic unchanged so that it gets handled by normal ASP.NET behind MVC. This is as trivial as adding an ignore route to your routing table in Gobal.asax.cs:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("elmah.axd");

Once all the above is done, you’ll see all unhandled exceptions that result in a yellowscreen-of-death be also logged into the XML files inside your App_Data and you can then watch them remotely by accessing http://server/elmah.axd. You’ll get a rather nice overview page like this one:

Congratulations, you have set up ELMAH on ASP.NET MVC and configured it to log all errors to XML Files. I strongly suggest you now read how to secure the Errorlog to prevent strangers from reading your logs.

Also, since 404 Errors in ASP.NET MVC are thrown as Exceptions, ELMAH will log them too, so you may want to read on how to filter those out

Comment by sethlo...@techie.com, Jun 16, 2009

Controllers in MVC use this method when an error occurs on an action:

protected override void OnException?(ExceptionContext? filterContext)

I had to handle this method and rethrow the error to get ELMAH logging to work when <customErrors mode= was set to "On".

Comment by sti...@gmail.com, Aug 20, 2009

Looks like ASP.net MVC 1.0 ignores axd files so you shouldn't need this extra step anymore.

Comment by alexander.prokofyev, Sep 23, 2009

I confirm what step 4 is unnecessary.

Comment by MrStimp...@gmail.com, Oct 9, 2009

Missing some details, found this on StackOverflow?:

    <system.web>
        <httpModules>
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
            <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
        </httpModules>
    </system.web>
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <modules runAllManagedModulesForAllRequests="true">
            <remove name="ErrorLog" />
            <remove name="ErrorMail" />
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
            <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
        </modules>
    </system.webServer>
Comment by hoelblin...@gmail.com, Oct 21, 2009

Hi there. I just wanted to point out that since MVC now ignores .axd files by default this article is no longer relevant.

Jon: The problem you are seeing is related to running ELMAH IIS7, not MVC in particular.

Comment by frost...@gmail.com, Nov 17, 2009

Do I need to do anything special to make it work with Visual Studio build-in web server(aka Cassini)? I get it working easily on IIS, but using same configuration, it doesn't work with Cassini. When I requested the log page via http://localhost:62364/elmah.axd, I had "The resource cannot be found."

Comment by achi...@gmail.com, Dec 24, 2009

I am facing the same issue, elmah.axd is not working on Cassini. I am using Windows 7 and Visual Studio 2008.

Comment by pup...@gmail.com, Jan 8, 2010

I'm using the filter from MvcContrib? which causes nice error pages, my own views, to be shown when an error occurs. The problem is that when that happens, ELMAH doesn't send me an email about the problem and I still would like to know that something went wrong.

It seems the key is in ExceptionContext?'s exceptionHandled. When set to true, ELMAH doesn't send the email and I get my own view, if I set it to false ELMAH does send the message but I get an ugly message from IIS. Any ideas how to show the nice view and get the email?

Comment by headhunt...@gmail.com, Jan 19, 2010

Handle the exception normally and then ErrorSignal?.FromCurrentContext?().Raise(ex); to tell ELMAH about it.

Comment by conceptworld@gmail.com, Mar 23, 2010

I added to system.web/httpHandlers & httpModules and it was working only on local machine and not on remote server.

Then I also added to system.webServer/handlers & modules to make it work.

Please see http://stackoverflow.com/questions/933554/elmah-not-working-with-asp-net-site

Thanks.

Comment by parasura...@gmail.com, May 8, 2010

We have implemented ELMAH in our application.It is running on windows server2008 IIS 7.our application is in UAT.If any changes happening in our sqlserver2008 its not getting reflected in our application.After we remove the ELMAH setting from the Webconfig file, the sqlserver2008 changes are getting reflected in our application. Please help us to solve this issue..

Comment by pderk...@gmail.com, Jun 20, 2010

To reiterate the link posted from conceptworld, here's the code needed to get ELMAH working in an ASP.NET MVC 2 project both locally and on a shared host:

<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="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    ...
  </httpModules>
  ...
</system.web>
<system.webServer>
  ...
  <modules runAllManagedModulesForAllRequests="true">
    ...
    <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
    <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    ...
  </modules>
  <handlers>
    ...
    <add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    ...
  </handlers>
<system.webServer>
Comment by jpcmor...@gmail.com, Aug 16, 2010

Is there any reason why the YSOD would not be saved in an ASP.NET MVC 2 application?

Comment by tdupont...@gmail.com, Oct 27, 2010

Slightly off topic, but CodeSmith? Insight provides the same advanced error reporting functionality as ELAMH, but it is designed to work with MVC right out of the box. Just wanted to throw that one out there. www.codesmithtools.com/product/insight

Comment by john.rum...@gmail.com, Feb 25, 2011

If your elmah handler isn't in the root of your site, e.g. admin/emlah.axd, you'll still need to add an ignore route such as the following:

routes.IgnoreRoute("admin/elmah.axd/{*pathInfo}");

The {*pathInfo} part is also necessary so that style sheets, rss, and other links work correctly.

Comment by alexande...@gmail.com, Mar 5, 2011

Here is an example of code I used to add elmah of native MVC controller:

http://www.beletsky.net/2011/03/integrating-elmah-to-aspnet-mvc-in.html

Comment by s...@naspinski.net, Apr 8, 2011

Mine would not work until I added this into my httpModules: <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />

Comment by gavinhar...@gmail.com, May 2, 2011

Anyone noticing that ELMAH isn't logging errors when used with MVC should also check out:

http://stackoverflow.com/questions/766610/how-to-get-elmah-to-work-with-asp-net-mvc-handleerror-attribute/779961#779961

Comment by c9952...@gmail.com, Jun 27, 2011

Just added it using NuGet?. It set-up my web.config so it just worked. No effort.

Comment by koraye...@gmail.com, Aug 19, 2011
Comment by alexande...@gmail.com, Aug 29, 2011

There is NuGet? package that delivers contoller in Admin area, to access ELMAH

http://nuget.org/List/Packages/Elmah.MVC

Comment by kiran.gc...@gmail.com, Dec 28, 2011

great !!

Comment by daredav...@gmail.com, Jan 15, 2012

<!-- Web.Config Configuration File -->

<configuration>

<system.web>
<customErrors mode="RemoteOnly"/>
</system.web>
</configuration>


Sign in to add a comment
Powered by Google Project Hosting