HttpClient
logging
disable logging
HTTP requests
.NET development

Disable HttpClient logging in .NET

Codemia Knowledge Hub ·

Set the log level for the System.Net.Http.HttpClient category to None. In .NET, HttpClient activity goes through the built-in logging framework, so you control it with logging configuration: appsettings.json, an environment variable, or a filter in code.

Turning it down is worth it because logging every request adds overhead under high load, requests often contain sensitive data that logs can expose, and the volume clutters logs and buries important diagnostics.

Set the log level in appsettings.json

In ASP.NET Core, logging is usually configured in appsettings.json. Lower the level for HttpClient or disable it entirely:

json
1    {
2      "Logging": {
3        "LogLevel": {
4          "Default": "Information",
5          "System.Net.Http.HttpClient": "None"
6        }
7      }
8    }

Set it with an environment variable

To change the level without touching code or configuration files, set an environment variable named Logging__LogLevel__System.Net.Http.HttpClient to None. The double underscores stand for the : separators, and the periods stay because they are part of the category name, so some shells need env or a similar tool to export it.

Add a filter in code

You can also configure logging in code when you register services, often in Startup.cs:

csharp
1    public void ConfigureServices(IServiceCollection services)
2    {
3        services.AddLogging(builder =>
4        {
5            builder.AddConsole();
6            builder.AddFilter("System.Net.Http.HttpClient", LogLevel.None);
7        });
8    }

Log selectively with a custom DelegatingHandler

Instead of disabling logging entirely, write a handler that decides what to log:

csharp
1    public class CustomDelegatingHandler : DelegatingHandler
2    {
3        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
4        {
5            // Custom logic here: Decide which info to log.
6            return await base.SendAsync(request, cancellationToken);
7        }
8    }

Attach it to your HttpClient instance. A handler created by hand needs an InnerHandler to pass the request on, or the first request throws InvalidOperationException:

csharp
1    var httpClient = new HttpClient(new CustomDelegatingHandler { InnerHandler = new HttpClientHandler() })
2    {
3        BaseAddress = new Uri("https://api.example.com/")
4    };

Keep detailed logs where you need them

  • Debugging: disabling logging completely can make debugging harder, especially in development.
  • Per environment: disable logging in production and keep detailed logs in development or staging.
  • Granularity: aim to keep the information you need while dropping excessive entries. Make changes incrementally and test their impact.
  • Log access: restrict access to any remaining logs to authorized people.
  • Review regularly: revisit logging configuration as the application scales and changes.
AspectExplanation
PerformanceDisabling unnecessary logs reduces application overhead.
SecurityPrevent logging of sensitive request and response data.
ConfigurationCan be adjusted via appsettings.json, environment variables, or programmatically.
Custom HandlersUse custom handlers to gain fine control over what is specifically logged.
Balanced ApproachFind a balance that suits both development and production needs.
Environment SpecificTailor logging configurations for different environments (dev, staging, prod).

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design