2014-06-17 2 views
2

ApiController에서 예외를 기록해야합니다.Elmah.MVC를 ApiController와 함께 사용하는 방법?

Elmah.MVC 설치 후 404 오류 및 기타 DB에 로그 할 수 있지만 ApiController의 예외는 Elmah에 표시되지 않습니다.

어떻게 해결할 수 있습니까?

<modules> 
     <remove name="FormsAuthenticationModule" /> 
     <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" /> 
     <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" /> 
     <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" /> 
    </modules> 

    <elmah> 
    <security allowRemoteAccess="yes" /> 
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="XXX"/> 
    </elmah> 



    public class StatusController : ApiController 
{ 
    private StatusBLL statusBLL = new StatusBLL(); 

    // POST api/status 
    public void Post(StatusDTO status) 
    { 
     throw new ArgumentException("test elmah here"); 
     if (!statusBLL.Register(status)) 
      helpers.BusinessLayer.CreateResponseApiMessage(statusBLL.Errors); 
    } 
} 
+0

관련 http://www.tugberkugurlu.com/archive/asp-net-web-api-and-elmah-integration – GibboK

답변

1

내가

public class ElmahErrorAttribute : ExceptionFilterAttribute 
{ 
    public override void OnException(
     System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext) 
    { 

     if (actionExecutedContext.Exception != null) 
      Elmah.ErrorSignal.FromCurrentContext().Raise(actionExecutedContext.Exception); 

     base.OnException(actionExecutedContext); 
    } 
} 


public class MvcApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     GlobalConfiguration.Configure(WebApiConfig.Register); // WebApi 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 
     Configure(System.Web.Http.GlobalConfiguration.Configuration); 
    } 
    private void Configure(HttpConfiguration httpConfiguration) 
    { 
     httpConfiguration.Filters.Add(
      new ElmahErrorAttribute() 
     ); 

    } 
} 
+0

이미 전역 필터에 HandleErrorAttribute가 등록되어 있습니까? – Tacoman667