如何在ApiController上使用Elmah.MVC?

Gib*_*boK 2 c# asp.net-mvc elmah elmah.mvc

我需要记录ApiController中的异常.

安装Elmah.MVC后,我可以在DB中记录404错误和其他错误,但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);   
    }
}
Run Code Online (Sandbox Code Playgroud)

Tho*_*dal 7

有一个NuGet包:http://www.nuget.org/packages/Elmah.Contrib.WebApi/

安装后,你需要添加一个新的全球性过滤器解释这里:http://blog.elmah.io/logging-to-elmah-io-from-web-api/(只需安装Elmah.Contrib.WebApi包不ELMAH .IO).