Pergunta

Normally in my Mvc project, I simply have a ErrorMail_Mailing method in Global.asax.cs which gives access to ErrorMailEventArgs.

But in WebApi, this method does not fire, so how can I access this information in Webapi?

I am currently using this methodology (which is working fine)

public class ApiErrorHandler : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext context)
        {
            DepResolver.ExceptionHelper().LogToElmah(context.Exception);
            base.OnException(context);
        }
    }
Foi útil?

Solução

ELMAH doesn't fire on Web API by default. You need to either catch the error yourself and log it to ELMAH or even better, use the Elmah.Contrib.WebApi NuGet package: http://www.nuget.org/packages/Elmah.Contrib.WebApi/. With that package installed, simply add the following code to your Application_Start:

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

This should trigger that your ErrorMail_Mailing method is called.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top