Web API 2中的错误日志记录建议

时间:2014-12-05 04:25:51

标签: asp.net-web-api2 error-logging

在大批量生产环境中,我应该为ASP.NET Web API 2.1使用哪种错误记录解决方案?

我试图让我的Web API尽可能轻量级,因此其中没有MVC。只是简单的旧Web API,如果可以,我想保持这种方式。

1 个答案:

答案 0 :(得分:0)

我来看看Elmah

http://www.asp.net/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/logging-error-details-with-elmah-cs

使用:http://www.nuget.org/packages/Elmah.Contrib.WebApi

此外,在Web Api中,您可以覆盖OnException,只需从那里调用记录器(Nlog或显式调用Elmah)。

public sealed class CustomExceptionFilterAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext actionExecutedContext)
    {
        yourLoggingMechanism.Log(actionExecutedContext.Exception);
        base.OnException(actionExecutedContext);
    }
}

然后在您的WebApiConfig类中,在Register方法中添加:

config.Filters.Add(new CustomExceptionFilterAttribute());

这些只是一些想法,决定在哪里登录(即对Db或Windows事件查看器等)真的取决于你正在做什么,你的决定可能会影响你是否想要用某些东西像Nlog一样。