A delegating handler for structured HTTP request, response, timing, header, and optional body logs.
dotnet add package Soenneker.HttpClients.LoggingHandlerusing Microsoft.Extensions.Logging;
using Soenneker.HttpClients.LoggingHandler;
var options = new HttpClientLoggingOptions
{
LogLevel = LogLevel.Debug,
LogRequestHeaders = true,
LogResponseHeaders = true,
LogRequestBody = false,
LogResponseBody = false
};
services.AddHttpClient("catalog", client =>
{
client.BaseAddress = new Uri("https://api.example.com/");
})
.AddHttpMessageHandler(serviceProvider =>
new HttpClientLoggingHandler(
serviceProvider.GetRequiredService<ILogger<HttpClientLoggingHandler>>(),
options));The handler logs the method, path, status code, and elapsed time. Header logging is enabled by default. Request and response bodies and query strings are disabled by default because they commonly contain credentials or personal data.
The default redaction list covers Authorization, Proxy-Authorization, Cookie, Set-Cookie, X-Api-Key, and Api-Key. Replace or extend RedactedHeaders before constructing the handler for application-specific secrets.
var options = new HttpClientLoggingOptions
{
LogResponseBody = true,
MaxBodyLogLength = 8_192,
RedactedHeaders =
[
"Authorization",
"Cookie",
"Set-Cookie",
"X-Api-Key",
"X-Tenant-Token"
]
};MaxBodyLogLength defaults to 4,096 characters. A negative value or int.MaxValue removes the limit and can buffer arbitrarily large content; use that only with payload sizes you control.
Limited body logging captures a bounded prefix as the transport or caller consumes the content, without eagerly reading or buffering it. The preview is logged when its byte budget fills, consumption completes, or the content is disposed. The complete body remains available downstream, including for non-seekable streams and unknown content lengths. Explicitly unlimited logging buffers and rewinds the complete body.
Set LogQueryString = true only when query parameters are known not to contain secrets. URI query values and body fields are not selectively redacted: when enabled, they are logged as supplied.