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"
    }
  }
}