29

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();

2 Answers 2

25

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I've set all my logging levels to warning, for everything, yet i still get information in a console. am i missing something here?
I figured out what the issue was, i redefined default loggers when i created a new Quarz Job that was doing all the HttpClient requests. Thanks For the answer!
14

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.

1 Comment

I tried this, in main appsettings and in appsettings.development.... it still shows me that output in a console o_0. Anything else may be overriding these?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.