如何自动记录.NET Core WebAPI中的每个请求? [英] How to auto log every request in .NET Core WebAPI?

查看:1252
本文介绍了如何自动记录.NET Core WebAPI中的每个请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每个请求都自动记录下来.在以前的.Net Framwork WebAPI项目中,我曾经注册过一个委托处理程序.

I'd like to have every request logged automatically. In previous .Net Framwork WebAPI project, I used to register a delegateHandler to do so.

WebApiConfig.cs

WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
    config.MessageHandlers.Add(new AutoLogDelegateHandler());
}

AutoLogDelegateHandler.cs

AutoLogDelegateHandler.cs

public class AutoLogDelegateHandler : DelegatingHandler
{

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var requestBody = request.Content.ReadAsStringAsync().Result;

        return await base.SendAsync(request, cancellationToken)
            .ContinueWith(task =>
            {
                HttpResponseMessage response = task.Result;

                //Log use log4net
                _LogHandle(request, requestBody, response);

                return response;
            });
    }
}

日志内容示例:

------------------------------------------------------
2017-08-02 19:34:58,840
uri: /emp/register
body: {
    "timeStamp": 1481013427,
    "id": "0322654451",
    "type": "t3",
    "remark": "system auto reg"
}
response: {"msg":"c556f652fc52f94af081a130dc627433","success":"true"}
------------------------------------------------------

但是在.NET Core WebAPI项目中,没有WebApiConfig或Global.asax GlobalConfiguration.Configure(WebApiConfig.Register);

But in .NET Core WebAPI project, there is no WebApiConfig , or the register function at Global.asax GlobalConfiguration.Configure(WebApiConfig.Register);

那么,在.NET Core WebAPI中有什么方法可以实现?

So is there any way to achieve that in .NET Core WebAPI?

推荐答案

您可以创建自己的过滤器属性...

You can create your own filter attribute...

public class InterceptionAttribute : ActionFilterAttribute
{
  public override void OnActionExecuting(HttpActionContext actionContext)
  {
    var x = "This is my custom line of code I need executed before any of the controller actions, for example log stuff";
    base.OnActionExecuting(actionContext);
  }
}

...,您将在GlobalFilters中注册它,但是由于您说的是使用.NET Core,因此可以尝试继续...

... and you would register it with GlobalFilters, but since you said you're using .NET Core, this is how you can try proceeding...

来自 docs.microsoft.com :

您可以全局注册过滤器(适用于所有控制器和操作) 通过将其添加到MvcOptions.Filters集合中 Startup类中的ConfigureServices方法:

You can register a filter globally (for all controllers and actions) by adding it to the MvcOptions.Filters collection in the ConfigureServices method in the Startup class:

让我们知道它是否有效.

Let us know if it worked.

P.S. 这是关于使用WebAPI拦截请求的整个教程,万一有人需要更多细节.

P.S. Here's a whole tutorial on intercepting requests with WebAPI, in case someone needs more details.

这篇关于如何自动记录.NET Core WebAPI中的每个请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆