ASP.NET Core中去除http请求的详细日志

2022-03-21  乐帮网

netcore

ASP.NET Core中去除Http日志。我的一个ASP项目运行起来后写了一堆有关Https的日志如下:


2022-03-18 11:17:51 INFO: Start processing HTTP request GET http://myapiserver/api/imgFolderPath/StudentInfo/153233684080627712.jpg
2022-03-18 11:17:51 INFO: Sending HTTP request GET http://myapiserver/api/imgFolderPath/StudentInfo/153233684080627712.jpg
2022-03-18 11:17:51 INFO: Received HTTP response headers after 54.9771ms - 200
2022-03-18 11:17:51 INFO: End processing HTTP request after 56.6098ms - 200
2022-03-18 11:17:51 INFO: Start processing HTTP request GET http://myapiserver/api/imgFolderPath/StudentInfo/153233684881739776.jpg
2022-03-18 11:17:51 INFO: Sending HTTP request GET http://myapiserver/api/imgFolderPath/StudentInfo/153233684881739776.jpg
2022-03-18 11:17:51 INFO: Received HTTP response headers after 62.5265ms - 200
2022-03-18 11:17:51 INFO: End processing HTTP request after 64.1143ms - 200
2022-03-18 11:17:51 INFO: Start processing HTTP request GET http://myapiserver/api/imgFolderPath/StudentInfo/153233679211040768.jpg
2022-03-18 11:17:51 INFO: Sending HTTP request GET http://myapiserver/api/imgFolderPath/StudentInfo/153233679211040768.jpg
2022-03-18 11:17:51 INFO: Received HTTP response headers after 50.9296ms - 200
2022-03-18 11:17:51 INFO: End processing HTTP request after 52.7231ms - 200
2022-03-18 11:17:51 INFO: Start processing HTTP request GET http://myapiserver/api/imgFolderPath/StudentInfo/153233546192883712.jpg
2022-03-18 11:17:51 INFO: Sending HTTP request GET http://myapiserver/api/imgFolderPath/StudentInfo/153233546192883712.jpg
2022-03-18 11:17:51 INFO: Received HTTP response headers after 68.6442ms - 200
2022-03-18 11:17:51 INFO: End processing HTTP request after 70.4435ms - 200

2022-03-18 14:54:37 TRACE: Request Headers:

2022-03-18 14:54:37 TRACE: Response Headers:
ETag: xv8Z1KlAOA2yHRc9O8S7klBPCk7gBnz3uQlCIPTea74
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Fri, 18 Mar 2022 06:54:05 GMT
Content-Length: 28619
Content-Type: image/jpeg

查询了官网,在ASP.NET Core6中我们以这样的方式来控制日志输出与不输出:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseHttpLogging();
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapGet("/", async context =>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    });
}

关键在这一句:   app.UseHttpLogging();

但是在ASP.NET Core5中,我发现即使没有这一句http日志还是输出。
解决办法是在我们的appsettings中添加过滤如下配置:

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      "System.Net.Http.HttpClient": "Warning"
    }
  }
}

 

公众号二维码

关注我的微信公众号
在公众号里留言交流
投稿邮箱:1052839972@qq.com

庭院深深深几许?杨柳堆烟,帘幕无重数。
玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮。门掩黄昏,无计留春住。
泪眼问花花不语,乱红飞过秋千去。

欧阳修

付款二维码

如果感觉对您有帮助
欢迎向作者提供捐赠
这将是创作的最大动力