如何在.NET Core控制台应用程序中启用HttpClient的日志记录?

如何在.NET Core控制台应用程序中启用HttpClient的日志记录?

问题描述:

.NET Core 2.2

我有一个使用 dotnet新控制台创建的基本CLI.它使用 HttpClient 发出Web请求.如何查看有关这些请求的跟踪或日志记录信息?

I have a basic CLI created with dotnet new console. It uses HttpClient to make web requests. How can I see trace or logging information about these requests?

像这样的数据: https://www.stevejgordon.co.uk/httpclientfactory-asp-net-core-logging

在.NET Framework中,您可以打开系统跟踪:

In the .NET Framework, you could turn on System Tracing: https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-configure-network-tracing

在您的appsettings.json文件中,将有一个类似于以下内容的对象:

In your appsettings.json file there will be an object which looks similar to the following:

  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  }

将日志级别增加到信息",然后再次运行您的应用程序.完成此操作后,请在 ASP中查看此页面.NET Core文档有关如何启用到文件或终端的日志记录.

Increase the log level to Information and run your application again. Once you have done that, take a look at this page in the ASP.NET Core documentation for how to enable logging to file or the terminal.