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?
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
I'd check your FilterConfig.cs class, it's possible that the default HandleErrorAttribute is being added there and is re-throwing your exception?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With