문제

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);
        }
    }
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top