Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 3.1 HttpClient to log only warnings and errors

I've noticed that my application(service) that supposed to run in a backgraund creates a log of garbage logging information because of HttpClient, like so:

info: System.Net.Http.HttpClient.Default.LogicalHandler[100] Start processing HTTP request POST https://localhost:44317/programmatic/getcontent info: System.Net.Http.HttpClient.Default.ClientHandler[100] Sending HTTP request POST https://localhost:44317/programmatic/getcontent info: System.Net.Http.HttpClient.Default.ClientHandler[101] Received HTTP response after 3027.6345ms - OK info: System.Net.Http.HttpClient.Default.LogicalHandler[101] End processing HTTP request after 3028.2987ms - OK info: System.Net.Http.HttpClient.Default.ClientHandler[101] Received HTTP response after 3052.4709ms - OK info: System.Net.Http.HttpClient.Default.LogicalHandler[101] End processing HTTP request after 3053.467ms - OK

Is there a way to configure it anywhere?

I inject client factory like this:

serviceCollection.AddHttpClient();

And then create a client like this:

HttpClient client = _clientFactory.CreateClient();
like image 325
AnKing Avatar asked Mar 30 '20 20:03

AnKing


People also ask

What is .NET Core ILogger?

The ILoggerFactory is the factory interface for creating an appropriate ILogger type instance and also for adding the ILoggerProvider instance. public interface ILoggerFactory : IDisposable { ILogger CreateLogger(string categoryName); void AddProvider(ILoggerProvider provider); }

Where does the log file get written to in .NET Core logging?

For example, the Console ILoggerProvider writes the logs to the console. This interface is used to create a custom instance of an ILogger . ILoggerFactory : This interface registers one or more ILoggerProvider instances and provides the CreateLogger() method used to create an instance of ILogger .

What is a HTTP logger?

HTTP Logging is a middleware that logs information about incoming HTTP requests and HTTP responses. HTTP logging provides logs of: HTTP request information. Common properties. Headers.


2 Answers

You can configure Logging in .NET Core through the Appsettings file. You should find a section in the appsettings.json file along the lines

{
  "Logging": {
    "Debug": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

You can add an additional Log Level filter to specify the minimum log level required to log.

{
  "Logging": {
    "Debug": {
      "LogLevel": {
        "Default": "Information",
        "System.Net.Http.HttpClient": "Debug"
      }
    }
  }
}

Documentation for Logging with filters in .NET Core can be found here.

Documemtation for Logging with filters in the IHttpClientFactory library can be found here. This documentation also includes examples of log filtering with a named HttpClient.

like image 138
kyleJ Avatar answered Oct 22 '22 05:10

kyleJ


You can override log level in appsettings.json by adding, for example, a new row to the Logging object:

  "Logging": {
    "LogLevel": {
      "System.Net.Http.HttpClient": "Warning"
    }
  },

This will log anything from Warning level and above.

like image 24
Mihaimyh Avatar answered Oct 22 '22 07:10

Mihaimyh