Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors are logged twice when using Elmah with WebApi

I'm trying to log exceptions from my asp.net web api project using elmah. I am having an issue where each error is logged twice.

I am using Elmah.Contrib.Web-Api and my Application class is as follows:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configuration.Filters.Add(new ElmahHandleErrorApiAttribute());

        RouteTable.Routes.MapHubs();
        RouteConfig.RegisterRoutes(RouteTable.Routes);

#if DEBUG
        EntityFrameworkProfiler.Initialize();
#endif

        GlobalConfig.CustomizeConfig(GlobalConfiguration.Configuration);

    }
}

If I commment out the following line then I get no messages at all:

GlobalConfiguration.Configuration.Filters.Add(new ElmahHandleErrorApiAttribute());

And I can confirm that I am only throwing one error and the call which generates the error is only been called once and I've not manually decorated my controllers or methods with the Elmah Attribute.

To try and resolve this I removed The Contrib Package and added followed the instructions found here http://www.tugberkugurlu.com/archive/asp-net-web-api-and-elmah-integration

This did not solve the issue and it still logs twice. It did allow me to put a break point into the Attribute class and confirm that for each error it is being called twice.

How can I solve this?

like image 609
Twisted Avatar asked Mar 12 '13 09:03

Twisted


2 Answers

What ELMAH-related entries are in your web.config?

I had a similar issue in an MVC application - handled exceptions were being logged twice. In the application I use a custom exception filter to log handled exceptions to ELMAH using error signalling, while the HTTP module takes care of unhandled exceptions.

It turned out that I needed to set:

<add key="elmah.mvc.disableHandleErrorFilter" value="true" />

in web.config in order to disable the built-in exception filter within the ELMAH.MVC NuGet package.

The source code for the built-in filter shows that it logs handled exceptions: https://github.com/alexanderbeletsky/elmah-mvc/blob/master/src/Elmah.Mvc/HandleErrorAttribute.cs

like image 176
DGreen Avatar answered Sep 19 '22 14:09

DGreen


I'd check your FilterConfig.cs class, it's possible that the default HandleErrorAttribute is being added there and is re-throwing your exception?

like image 38
Po-ta-toe Avatar answered Sep 21 '22 14:09

Po-ta-toe