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

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

问题描述

我希望自动记录每个请求.在之前的 .Net Framwork WebAPI 项目中,我曾经注册了一个 delegateHandler 来这样做.

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.

附言这是一个关于使用 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天全站免登陆