Microsoft.Extensions.Http.Diagnostics 11.0.0
Microsoft.Extensions.Http.Diagnostics
Telemetry support for HttpClient that allows tracking latency and enriching and redacting log output for structured logs.
Install the package
From the command-line:
dotnet add package Microsoft.Extensions.Http.Diagnostics
Or directly in the C# project file:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http.Diagnostics" Version="[CURRENTVERSION]" />
</ItemGroup>
Usage Example
HTTP Client Logs Enrichment and Redaction
These components enable enriching and redacting HttpClient request logs. They remove built-it HTTP Client logging.
When using this package, some of the log properties are redacted by default (like full routes). AddExtendedHttpClientLogging() registers a default redactor provider by calling AddRedaction() from the Microsoft.Extensions.Compliance.Redaction package. The default redactor provider erases any data that has a data classification other than DataClassification.None. To use specific redactors for your data classifications, call builder.Services.AddRedaction(...) and configure them; this can be done before or after calling AddExtendedHttpClientLogging(). If you use AddFakeRedaction() or a custom IRedactorProvider implementation, it must be registered before calling AddExtendedHttpClientLogging().
The http client logging services can be registered using the following methods:
public static IServiceCollection AddExtendedHttpClientLogging(this IServiceCollection services)
public static IServiceCollection AddExtendedHttpClientLogging(this IServiceCollection services, IConfigurationSection section)
public static IServiceCollection AddExtendedHttpClientLogging(this IServiceCollection services, Action<LoggingOptions> configure)
public static IServiceCollection AddHttpClientLogEnricher<T>(this IServiceCollection services) where T : class, IHttpClientLogEnricher
For example:
var builder = Host.CreateApplicationBuilder(args);
// Register IHttpClientFactory:
builder.Services.AddHttpClient();
// Register HttpClient logging enrichment & redaction services:
builder.Services.AddExtendedHttpClientLogging();
// Register a logging enricher (the type should implement IHttpClientLogEnricher):
builder.Services.AddHttpClientLogEnricher<MyHttpClientLogEnricher>();
var host = builder.Build();
It is important to note that the AddExtendedHttpClientLogging method will add information to the logs using enrichment. This means that the information will be added as tags to the structured logs, but will not be visible in the log message that is printed by default in the console. To view the information, you will need to use a logging provider that supports structured logs. One quick and built-in way to do this, is to call AddJsonConsole() to your logging builder, which will print out the full structured logs to the console. Here is a quick sample that uses the ExtendedHttpClientLogging() method to automatically log all HttpClient request and response bodies, and then prints the full structured logs to the console:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
var services = new ServiceCollection();
services.AddLogging(o => o.SetMinimumLevel(LogLevel.Trace).AddJsonConsole()); // <-- Enable structured logging to the console
services.AddHttpClient("foo")
.AddExtendedHttpClientLogging(o =>
{
// Enable logging of request and response bodies:
o.LogBody = true;
// We also need to specify the content types that we want to log:
o.ResponseBodyContentTypes.Add("application/json");
});
var sp = services.BuildServiceProvider();
var client = sp.GetRequiredService<IHttpClientFactory>().CreateClient("foo");
var response = await client.GetAsync(new Uri("https://httpbin.org/json")).ConfigureAwait(false);
By default, request and response routes are redacted for privacy reasons. You can change this behavior by making use of the RequestPathParameterRedactionMode option like:
.AddExtendedHttpClientLogging(o =>
{
//.. Other options
o.RequestPathParameterRedactionMode = HttpRouteParameterRedactionMode.None; // <-- Disable redaction of request/response routes
});
You can also use the following extension methods to apply the logging to the specific IHttpClientBuilder:
public static IHttpClientBuilder AddExtendedHttpClientLogging(this IHttpClientBuilder builder)
public static IHttpClientBuilder AddExtendedHttpClientLogging(this IHttpClientBuilder builder, IConfigurationSection section)
public static IHttpClientBuilder AddExtendedHttpClientLogging(this IHttpClientBuilder builder, Action<LoggingOptions> configure)
For example:
var builder = Host.CreateApplicationBuilder(args);
// Register named HttpClient:
var httpClientBuilder = builder.Services.AddHttpClient("MyNamedClient");
// Configure named HttpClient to use logging enrichment & redaction:
httpClientBuilder.AddExtendedHttpClientLogging();
var host = builder.Build();
Tracking HTTP Request Client Latency
These components enable tracking and reporting the latency of HTTP Client request processing.
The services can be registered using the following methods. They also register the latency context services (see AddLatencyContext()) unless those are already registered:
public static IServiceCollection AddHttpClientLatencyTelemetry(this IServiceCollection services)
public static IServiceCollection AddHttpClientLatencyTelemetry(this IServiceCollection services, IConfigurationSection section)
public static IServiceCollection AddHttpClientLatencyTelemetry(this IServiceCollection services, Action<HttpClientLatencyTelemetryOptions> configure)
For example:
var builder = Host.CreateApplicationBuilder(args);
// Register IHttpClientFactory:
builder.Services.AddHttpClient();
// Register HttpClient logging enrichment & redaction services:
builder.Services.AddExtendedHttpClientLogging();
// Register HttpClient latency telemetry services:
builder.Services.AddHttpClientLatencyTelemetry();
var host = builder.Build();
Feedback & Contributing
We welcome feedback and contributions in our GitHub repo.
Supported Platforms
This package includes .NET Framework compatible targets, but is not officially supported on .NET Framework.
Showing the top 20 packages that depend on Microsoft.Extensions.Http.Diagnostics.
| Packages | Downloads |
|---|---|
|
Microsoft.Extensions.Http.Resilience
Resilience mechanisms for HttpClient.
|
7 |
|
Microsoft.Extensions.Http.Resilience
Resilience mechanisms for HttpClient.
|
8 |
|
Microsoft.Extensions.Http.Resilience
Resilience mechanisms for HttpClient.
|
9 |
|
Microsoft.Extensions.Http.Resilience
Resilience mechanisms for HttpClient.
|
20 |
|
Microsoft.Extensions.Http.Resilience
Resilience mechanisms for HttpClient.
|
27 |
.NET 10.0
- Microsoft.Extensions.Compliance.Redaction (>= 11.0.0)
- Microsoft.Extensions.Telemetry (>= 11.0.0)
- Microsoft.Extensions.Http (>= 10.0.15)
.NET 11.0
- Microsoft.Extensions.Compliance.Redaction (>= 11.0.0)
- Microsoft.Extensions.Telemetry (>= 11.0.0)
- Microsoft.Extensions.Http (>= 11.0.0)
.NET Framework 4.6.2
- Microsoft.Extensions.Compliance.Redaction (>= 11.0.0)
- Microsoft.Extensions.Telemetry (>= 11.0.0)
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- System.IO.Pipelines (>= 8.0.0)
.NET 8.0
- Microsoft.Extensions.Compliance.Redaction (>= 11.0.0)
- Microsoft.Extensions.Telemetry (>= 11.0.0)
- Microsoft.Extensions.Http (>= 8.0.1)
- System.IO.Pipelines (>= 8.0.0)
.NET 9.0
- Microsoft.Extensions.Compliance.Redaction (>= 11.0.0)
- Microsoft.Extensions.Telemetry (>= 11.0.0)
- Microsoft.Extensions.Http (>= 9.0.23)
.NET Standard 2.0
- Microsoft.Extensions.Compliance.Redaction (>= 11.0.0)
- Microsoft.Extensions.Telemetry (>= 11.0.0)
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- System.IO.Pipelines (>= 8.0.0)
| Version | Downloads | Last updated |
|---|---|---|
| 11.0.1 | 0 | 11/21/2026 |
| 11.0.0 | 2 | 11/15/2026 |
| 10.11.1 | 0 | 11/20/2026 |
| 10.11.0 | 3 | 10/16/2026 |
| 10.10.0 | 4 | 09/18/2026 |
| 10.9.0 | 5 | 08/30/2026 |
| 10.8.0 | 10 | 07/20/2026 |
| 10.7.0 | 5 | 06/14/2026 |
| 10.6.0 | 6 | 05/17/2026 |
| 10.5.0 | 8 | 04/16/2026 |
| 10.4.0 | 8 | 04/16/2026 |
| 10.3.0 | 17 | 03/30/2026 |
| 10.2.0 | 7 | 04/25/2026 |
| 10.1.0 | 7 | 04/25/2026 |
| 10.0.0 | 8 | 03/30/2026 |
| 9.10.0 | 7 | 04/25/2026 |
| 9.9.0 | 7 | 04/25/2026 |
| 9.8.0 | 7 | 04/25/2026 |
| 9.7.0 | 7 | 04/25/2026 |
| 9.6.0 | 7 | 04/25/2026 |
| 9.5.0 | 7 | 04/25/2026 |
| 9.4.0 | 7 | 04/25/2026 |
| 9.3.0 | 7 | 04/25/2026 |
| 9.2.0 | 7 | 04/25/2026 |
| 9.1.0 | 7 | 04/25/2026 |
| 9.0.0 | 7 | 04/25/2026 |
| 9.0.0-preview.9.24507.7 | 7 | 04/25/2026 |
| 9.0.0-preview.8.24460.1 | 7 | 04/25/2026 |
| 9.0.0-preview.7.24412.10 | 7 | 04/25/2026 |
| 9.0.0-preview.6.24353.1 | 7 | 04/25/2026 |
| 9.0.0-preview.5.24311.7 | 7 | 04/25/2026 |
| 9.0.0-preview.4.24271.2 | 7 | 04/25/2026 |
| 9.0.0-preview.3.24209.3 | 7 | 04/25/2026 |
| 9.0.0-preview.2.24157.4 | 7 | 04/25/2026 |
| 9.0.0-preview.1.24108.1 | 7 | 04/25/2026 |
| 8.10.0 | 7 | 04/25/2026 |
| 8.9.1 | 7 | 04/25/2026 |
| 8.9.0 | 7 | 04/25/2026 |
| 8.8.0 | 7 | 04/25/2026 |
| 8.7.0 | 7 | 04/25/2026 |
| 8.6.0 | 7 | 04/25/2026 |
| 8.5.0 | 7 | 04/24/2026 |
| 8.4.0 | 7 | 04/25/2026 |
| 8.3.0 | 7 | 04/25/2026 |
| 8.2.0 | 7 | 04/25/2026 |
| 8.1.0 | 7 | 04/25/2026 |
| 8.0.0 | 7 | 04/25/2026 |