Microsoft.Extensions.Http.Diagnostics 9.0.0-preview.2.24157.4

Prefix Reserved
This is a prerelease version of Microsoft.Extensions.Http.Diagnostics.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Microsoft.Extensions.Http.Diagnostics --version 9.0.0-preview.2.24157.4                
NuGet\Install-Package Microsoft.Extensions.Http.Diagnostics -Version 9.0.0-preview.2.24157.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Microsoft.Extensions.Http.Diagnostics" Version="9.0.0-preview.2.24157.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Microsoft.Extensions.Http.Diagnostics --version 9.0.0-preview.2.24157.4                
#r "nuget: Microsoft.Extensions.Http.Diagnostics, 9.0.0-preview.2.24157.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Microsoft.Extensions.Http.Diagnostics as a Cake Addin
#addin nuget:?package=Microsoft.Extensions.Http.Diagnostics&version=9.0.0-preview.2.24157.4&prerelease

// Install Microsoft.Extensions.Http.Diagnostics as a Cake Tool
#tool nuget:?package=Microsoft.Extensions.Http.Diagnostics&version=9.0.0-preview.2.24157.4&prerelease                

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), which means that you will need to make sure that a redactor provider is registered in the Dependency Injection container. You can do this by making sure that you call builder.Services.AddRedaction() which requires a reference to the Microsoft.Extensions.Compliance.Redaction package.

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 redaction services:
builder.Services.AddRedaction();

// 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

// Adding default redactor provider to the DI container. This is required when using the AddExtendedHttpClientLogging() method.
services.AddRedaction();

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 redaction services:
builder.Services.AddRedaction();

// 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:

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 redaction services:
builder.Services.AddRedaction();

// Register latency context services:
builder.Services.AddLatencyContext();

// 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.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Microsoft.Extensions.Http.Diagnostics:

Package Downloads
Microsoft.Extensions.Http.Resilience

Resilience mechanisms for HttpClient.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.0 82,921 11/12/2024
9.0.0-preview.9.24507.7 24,051 10/8/2024
9.0.0-preview.8.24460.1 17,365 9/10/2024
9.0.0-preview.7.24412.10 5,913 8/14/2024
9.0.0-preview.6.24353.1 4,847 7/10/2024
9.0.0-preview.5.24311.7 5,194 6/11/2024
9.0.0-preview.4.24271.2 6,421 5/21/2024
9.0.0-preview.3.24209.3 7,899 4/11/2024
9.0.0-preview.2.24157.4 3,453 3/12/2024
9.0.0-preview.1.24108.1 2,004 2/13/2024
8.10.0 679,042 10/8/2024
8.9.1 692,287 9/6/2024
8.9.0 42,079 9/5/2024
8.8.0 591,838 8/13/2024
8.7.0 900,315 7/10/2024
8.6.0 637,942 6/11/2024
8.5.0 772,443 5/14/2024
8.4.0 1,194,178 4/9/2024
8.3.0 511,294 3/12/2024
8.2.0 849,164 2/13/2024
8.1.0 359,023 1/9/2024
8.0.0 568,547 11/14/2023