关闭 .net core 3.1 中的 HttpClient 登录

我在 asp.net core3.1 中创建了一个 http 客户端。并通过启动注入它,它工作正常。

services.AddHttpClient<MyClient>();

但是我无法控制它的日志输出。一直遵循这里的建议 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1#logging

我在我的配置中尝试了各种方法。目前看起来像这样

  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning",
      "System": "Error",
      "System.Net.Http": "Warning",
      "System.Net.Http.HttpClient": "Warning",
      "System.Net.Http.HttpClient.MyClient.LogicalHandler": "Warning",
      "System.Net.Http.HttpClient.MyClient.ClientHandler": "Warning",
      "Microsoft.AspNetCore.Hosting": "Warning",
      "Microsoft.Hosting.Lifetime": "Warning"
    }
  },

但我不断获取信息日志,例如

Level":"Information","MessageTemplate":"Received HTTP response after {ElapsedMilliseconds}ms - {StatusCode}","Properties":{"ElapsedMilliseconds":895.4859,"StatusCode":"UnprocessableEntity","EventId":{"Id":101,"Name":"RequestEnd"}

"Level":"Information","MessageTemplate":"End processing HTTP request after {ElapsedMilliseconds}ms - {StatusCode}","Properties":{"ElapsedMilliseconds":932.1484,"StatusCode":"UnprocessableEntity","EventId":{"Id":101,"Name":"RequestPipelineEnd"}

sourcecontext 也在记录。这记录为 "SourceContext":"System.Net.Http.HttpClient.MyClient.ClientHandler" 在我看来排除了打字问题

stack overflow Turn off HttpClient Logging in .net core 3.1
原文答案

相关问题