Search code examples
c#.netloggingdotnet-httpclient.net-6.0

Reducing HttpClient log verbosity


I'm currently writing a .NET 6 application which makes some REST calls.

For some reason, when these calls are made, HttpClient is logging the following:

[15:33:15 INF] Start processing HTTP request GET URL_GOES_HERE
[15:33:15 INF] Sending HTTP request GET URL_GOES_HERE
[15:33:15 INF] Received HTTP response headers after 70.5393ms - 200
[15:33:15 INF] End processing HTTP request after 73.441ms - 200

Due to the number of calls I'm making, this is making my logging platform hard to navigate.

I can't find any documentation online as to how I'd go about muting these logs. They seem to be new to .NET 6? At least I haven't seen them before starting this new project.

I found this github repo which solves this problem, however, I'd rather not pull a third-party package in just to silence some logs.

I have other "information" level logs in my application, meaning I'm unable to suppress the entire level.

Is there really no native way to silence these logs from HttpClient?


Solution

  • I managed to exclude these logs using Serilog, which I was already using in the application.

    When setting up the LoggerConfiguration, you can apply "filters", like so:

    Log.Logger = new LoggerConfiguration()
        .Filter.ByExcluding("SourceContext like '%System.Net.Http.HttpClient%' and @l = 'Information'")
        .CreateLogger();