c# - 如何将 Elmah.MVC 与 ApiController 一起使用?

标签 c# asp.net-mvc elmah elmah.mvc

我需要记录来自 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/

相关文章:

asp.net - ELMAH 未登录 ASP.NET MVC 2

asp.net-mvc - 在单个事务上包装成员资格提供程序和 dbcontext

c# - Entity Framework with LINQ 是否符合此 STIG?

asp.net-mvc - AngularJS 在 Release模式下不工作(缩小)

c# - 当 HttpContext.Current 为空时,如何在 Elmah 的 ErrorMail_Mailing 事件中的 C# 中获取服务器变量(或至少客户端 IP 地址)?

c# - LINQ 中的 "RemoveAll"怎么可能比迭代快得多?

c# - 并排配置错误,无法解析引用 mscorlib

c# - 检查 Dictionary 在其任何键中是否具有值

c# - linq中的动态属性名称

c# - 如何记录具有完整调用堆栈的异常?