3

Background

I have an ASP.NET Core 5.0 application. Part of its functionality talks to a web service to sync some of its data.

I do this using the following code in my service:

FooRequest fr = new FooRequest();
var request = new HttpRequestMessage(HttpMethod.Get, "https://example.com/fooapiurl);
request.Headers.Add("Accept", "application/json");
request.Headers.Add("User-Agent", "foo-user-agent");
request.Headers.Add("FooRequest", JsonSerializer.Serialize(fr));
LogInfo(4, "Created http request");
var client = _clientFactory.CreateClient();
LogInfo(4, "Created http client");
var clientResponse = await client.SendAsync(request);
LogInfo(4, "Got http client response");

LogInfo is a simple helper function as follows

private void LogInfo(int logLevel, string logMessage)
{
    if (logLevel <= _logLevel) _logger.LogInformation(logMessage);
}

_logLevel is a private variable I set from appsettings.json, using IOptions<T> dependency injection. I can configure this setting from 0 to 5 and it allows me scope to troubleshoot the service as and when I need to see more logging. _logLevel = Config.LogLevel

Startup middleware

services.AddHttpClient();

This all works well apart from the logging that gets turned on automatically using _clientFactory.CreateClient.

2021-06-16 13:05:02.6940|System.Net.Http.HttpClient.Default.LogicalHandler|INFO|Start processing HTTP request GET https://example.com/fooapiurl
2021-06-16 13:05:02.6940|System.Net.Http.HttpClient.Default.ClientHandler|INFO|Sending HTTP request GET https://example.com/fooapiurl
2021-06-16 13:05:02.7828|System.Net.Http.HttpClient.Default.ClientHandler|INFO|Received HTTP response headers after 83.9028ms - 200
2021-06-16 13:05:02.7828|System.Net.Http.HttpClient.Default.LogicalHandler|INFO|End processing HTTP request after 92.2096ms - 200

I like the way logs are on by default but not in this particular instance.

Requirement

  • I want to keep the noise of the logging down to a minimum as I'm running this process every 5 minutes.
  • I only want the HttpClient information logs to output when I set my log level to 4, in line with the other information messages I'm logging (see service code extract above).

Investigation

I've seen that you can control the whole logging of HttpClient by doing the following which does turn off the logs. However, this doesn't allow me to control the logs based on my log level.

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

I've also found examples where you can Tidy up your HttpClient usage and Logging request/response messages when using HttpClient so I'm aware the facility's there for you to create custom HttpMessageHandler or instantiate the middleware with alternate settings to alter the logs that HttpClient outputs. However, neither examples pass in a way to configure using a log level.

Question

What's the best way for me to implement my requirement?

Neil
  • 601
  • 4
  • 20
  • In the `appsettings.json` file, try to set the `loglevel` as `"Logging": { "LogLevel": { "Default": "Information", "System.Net.Http.HttpClient": "Warning" } }`, then, in the service or controller, use the `LogInformation()` method or `Log()` method to add Information messages. More detail information, see [Log Level](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0#log-level). – Zhi Lv Jun 17 '21 at 07:50

0 Answers0