如何使用NLog跟踪每个请求ASP.NET Web API

Bas*_*mme 6 c# asp.net json nlog

我使用ASP.NET Web API创建了一个简单的REST API.出于测试目的,我想添加一些跟踪.所以我在项目中添加了NLog.此时我的日志记录是这样的:

// POST api/values
public void Post([FromBody]string value)
{
    logger.Trace("Request: {0} api/values", Request.Method); 
    _repository.insert(value);
    logger.Trace("Response: {0} api/values", Request.Method); 
}
Run Code Online (Sandbox Code Playgroud)

在每种方法中,我都在方法的顶部和底部添加了一个logger.Trace.这个方法有2个问题:

  1. 我需要记住将这些行添加到我的每个方法中
  2. 我不知道如何将JSON主体添加到我的跟踪中

点1现在不是一个真正的问题(见下文),但我很快就需要检查我的API接收到的每个JSON主体.

我已经试过了

// POST api/values
public void Post([FromBody]string value)
{
    logger.Trace("Request: {0} api/values {1}", Request.Method, Request.Body); 
    _repository.insert(value);
    logger.Trace("Response: {0} api/values", Request.Method); 
}
Run Code Online (Sandbox Code Playgroud)

但请求中没有Body属性.

我还为我的观点1找到了一个有趣的文档:http://weblogs.asp.net/fredriknormen/log-message-request-and-response-in-asp-net-webapi

Leo*_*Leo 7

这就是你有动作过滤器...在动作方法执行/执行之前/之后做某事

public class MyCustomFilter : System.Web.Http.Filters.ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        //Do something here before an action method starts executing
    }

    public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext context)
    {
        //Do something here after an action method finished executing
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,你需要在asp.net管道中插入这个过滤器......当应用程序启动时,无论你使用owin/katana还是global.asax都无关紧要......

GlobalConfiguration.Configuration.Filters.Add(new MyCustomFilter());
Run Code Online (Sandbox Code Playgroud)

上面的行会将该过滤器添加到所有操作方法中.如果要关闭某些操作方法的跟踪,只需将一个标志/开关属性添加到操作过滤器,以便您可以关闭某些操作的跟踪...

public class MyCustomFilter : System.Web.Http.Filters.ActionFilterAttribute
{
    public bool DisableTracing{get;set;}

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if(!DisableTracing){
               //Do something here before an action method starts executing
        }
    }

    public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext context)
    {
        if(!DisableTracing){
               //Do something here before an action method starts executing
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在你可以通过控制器动作来解决它......

[MyCustomFilter(DisableTracing = true)]
public IHttpActionResult MyAction(int id){}
Run Code Online (Sandbox Code Playgroud)

更新

要从请求的正文中读取JSON对象,只需读取请求的内容,如下所示......

 request.Content.ReadAsStringAsync().Result;
Run Code Online (Sandbox Code Playgroud)


Bas*_*mme 3

Leo 的解决方案对于 MVC 似乎是正确的,但对于 Http REST API,我必须实现http://www.c-sharpcorner.com/UploadFile/1492b1/restful-day-sharp6-request-logging-and-exception-的解决方案处理登录/

public class HttpLoggingFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext filterContext)
    {
        //Do something here
    }

    public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
    {
        //Do something here
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的代码上测试这两种方法后,我可以看出 Leo 的代码是在页面刷新位上执行的,而不是在简单的 REST 请求上执行的。