Search code examples
c#dotnet-httpclientseriloghttpclientfactory

How to configure serilog using .json configuration file to avoid the messages from HttpClientFactory?


I have an application that use HttpClientFactory to create a HttpClient to makes requests.

I would like to avoid to log the information messages from HttpClient and HttpClientFactory.

To avoid messages from HttpClient in serilog, I have this configuration in the appsettings.json file:

"Serilog": {
  "MinimumLevel": {
    "Default": "Verbose",
    "Override": {
      "System.Net.Http.HttpClient": "Warning"
    }
  },

This solve the messages when I do a request with the http client.

However, I am still having this messages:

11:00:50 DBG] HttpMessageHandler expired after 120000ms for client ''
[11:01:00 DBG] Starting HttpMessageHandler cleanup cycle with 1 items
[11:01:00 DBG] Ending HttpMessageHandler cleanup cycle after 0.0323ms - processed: 0 items - remaining: 1 items
[11:01:10 DBG] Starting HttpMessageHandler cleanup cycle with 1 items
[11:01:10 DBG] Ending HttpMessageHandler cleanup cycle after 0.0037ms - processed: 0 items - remaining: 1 items
[11:01:20 DBG] Starting HttpMessageHandler cleanup cycle with 1 items
[11:01:20 DBG] Ending HttpMessageHandler cleanup cycle after 0.0025ms - processed: 0 items - remaining: 1 items
[11:01:30 DBG] Starting HttpMessageHandler cleanup cycle with 1 items
[11:01:30 DBG] Ending HttpMessageHandler cleanup cycle after 0.0034ms - processed: 0 items - remaining: 1 items
[11:01:40 DBG] Starting HttpMessageHandler cleanup cycle with 1 items
[11:01:40 DBG] Ending HttpMessageHandler cleanup cycle after 0.0037ms - processed: 0 items - remaining: 1 items
[11:01:50 DBG] Starting HttpMessageHandler cleanup cycle with 1 items
[11:01:50 DBG] Ending HttpMessageHandler cleanup cycle after 0.0033ms - processed: 0 items - remaining: 1 items

If I am not wrong, this messages come from the HttpClientFactory when it handles the life cycle of the HttMessageHandlers.

How could I avoid to log this in a similar way I avoid to log the messages from the HttpClient?


Solution

  • These messages are logged by the DefaultHttpClientFactory.

    So, if you don't want to see these messages then set the minimum log level to Information for the DefaultHttpClientFactory:

    "Microsoft.Extensions.Http.DefaultHttpClientFactory": "Information"
    

    As you can see the DefaultHttpClientFactory resides inside the Microsoft.Extensions.Http namespace, not under the System.Net.Http like the HttpClient.