Errors are logged twice when using Elmah with WebApi

寵の児 提交于 2020-01-13 08:55:42

问题


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?


回答1:


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




回答2:


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




回答3:


For what it's worth, I was having the same problem of ELMAH logging each exception twice in my Web API application (using Elmah.Contrib.WebApi).

Comparing my ELMAH emails against my source history, I was able to identify that the problem started happening after the Ninject.Web.WebApi 3.0.2-unstable-9016 package was installed via nuget.

Sure enough, uninstalling the package and commenting out the single dependency binding that was using it solved the double exception logging problem. Reinstalling the package and leaving the dependency binding commented out caused the problem to start again, so it wasn't the binding itself.

Installing the previous version (Ninject.Web.WebApi 3.0.2-unstable-8) did not cause the problem to happen, but my dependency binding no longer worked.

I'm choosing to live with the problem for the time being.




回答4:


Have you seen this post ? The author uses an ExceptionFilter to handle logging exceptions




回答5:


For other folks with the logging twice issue (I don't think this helps the OP?) - this happened to me and the reason was because I had applied the filter globally

GlobalConfiguration.Configuration.Filters.Add(new ElmahErrorAttribute()); //log web api errors

but also applied the attribute to the class (doh!)

[ElmahError]
public class TestController : ApiController

So of course it logged twice.




回答6:


I had this problem occur only on HTTP 401 responses. The issue turned out to be that Windows Authentication was enabled which was causing the browser to make a second negotiation request.

In my case, I was just able to disable Windows Authentication in the web.config:

<security>
  <authentication>
    <windowsAuthentication enabled="false" />
  </authentication>
</security>

Note: If you don't have the config section unlocked, you can just disable Windows Authentication in IIS.



来源:https://stackoverflow.com/questions/15357497/errors-are-logged-twice-when-using-elmah-with-webapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!