我需要记录来自 ApiController 的异常。
安装后Elmah.MVC我可以在数据库中记录 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);
}
}
最佳答案
有一个 NuGet 包:http://www.nuget.org/packages/Elmah.Contrib.WebApi/
安装后您需要添加一个新的全局过滤器,如下所述:http://blog.elmah.io/logging-to-elmah-io-from-web-api/ (只需安装没有 elmah.io 的 Elmah.Contrib.WebApi 包)。
关于c# - 如何将 Elmah.MVC 与 ApiController 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24257616/