extensions icon indicating copy to clipboard operation
extensions copied to clipboard

AddExtendedHttpClientLogging(configurationSection) fails with the documented appsettings.json sample for RequestHeadersDataClasses

Open Marcus-Kanon opened this issue 5 months ago • 0 comments

### Description

AddExtendedHttpClientLogging(configurationSection) fails during startup when RequestHeadersDataClasses is configured from appsettings.json.

This appears to contradict the documented configuration path in the HTTP client logging docs: https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-logging?tabs=dotnet-cli#configure-from-appsettingsjson

The docs show this configuration:

{
  "HttpClientLogging": {
    "LogRequestStart": false,
    "LogBody": false,
    "BodySizeLimit": 32768,
    "BodyReadTimeout": "00:00:01",
    "RequestHeadersDataClasses": {
      "User-Agent": "None",
      "Content-Type": "None"
    },
    "ResponseHeadersDataClasses": {
      "Content-Type": "None"
    },
    "RequestPathLoggingMode": "Formatted",
    "RequestPathParameterRedactionMode": "Strict"
  }
}

and this registration:

builder.Services.AddExtendedHttpClientLogging(
    builder.Configuration.GetSection("HttpClientLogging"));

Reproduction Steps

  1. Create a minimal app using Microsoft.Extensions.Http.Diagnostics 10.6.0.
  2. Register redaction services with services.AddRedaction();.
  3. Register a named client with configuration-based HTTP client logging:
services.AddHttpClient("test")
    .AddExtendedHttpClientLogging(configuration.GetSection("HttpClientLogging"));
  1. Use the documented appsettings.json example from the docs.
  2. Build the service provider and create the client.

Minimal repro:

using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using var stream = new MemoryStream(Encoding.UTF8.GetBytes("""
{
  "HttpClientLogging": {
    "LogRequestStart": false,
    "LogBody": false,
    "BodySizeLimit": 32768,
    "BodyReadTimeout": "00:00:01",
    "RequestHeadersDataClasses": {
      "User-Agent": "None",
      "Content-Type": "None"
    },
    "ResponseHeadersDataClasses": {
      "Content-Type": "None"
    },
    "RequestPathLoggingMode": "Formatted",
    "RequestPathParameterRedactionMode": "Strict"
  }
}
"""));

var configuration = new ConfigurationBuilder()
    .AddJsonStream(stream)
    .Build();

var services = new ServiceCollection();
services.AddLogging();
services.AddRedaction();

services.AddHttpClient("test")
    .AddExtendedHttpClientLogging(configuration.GetSection("HttpClientLogging"));

using var serviceProvider = services.BuildServiceProvider();
_ = serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient("test");

Expected behavior

The documented appsettings.json sample should bind successfully and the application should start.

RequestHeadersDataClasses and ResponseHeadersDataClasses should bind from configuration using values like "None" and "Unknown", as shown in the documentation.

Actual behavior

Startup fails while binding Microsoft.Extensions.Compliance.Classification.DataClassification.

Observed exception:

InvalidOperationException: Cannot create instance of type 'Microsoft.Extensions.Compliance.Classification.DataClassification' because parameter 'taxonomyName' has no matching config. Each parameter in the constructor that does not have a default value must have a corresponding config entry.

Additional isolated repro results:

  • The docs-shaped sample fails as-is.
  • User-Agent: "None" alone fails.
  • Authorization: "Unknown" alone fails.
  • Object-shaped attempts also fail, for example:
{
  "HttpClientLogging": {
    "RequestHeadersDataClasses": {
      "User-Agent": {
        "taxonomyName": "",
        "value": "None"
      }
    }
  }
}

with:

ArgumentException: Argument is whitespace (Parameter 'taxonomyName')

Regression?

Unknown. I have not yet tested earlier package versions.

Known Workarounds

Code-based configuration works:

services.AddHttpClient("test")
    .AddExtendedHttpClientLogging(options =>
    {
        options.RequestHeadersDataClasses.Add("User-Agent", DataClassification.None);
        options.RequestHeadersDataClasses.Add("Content-Type", DataClassification.None);
    });

So the problem appears specific to the configuration-binding path.

Configuration

  • Package: Microsoft.Extensions.Http.Diagnostics 10.6.0
  • SDK: .NET SDK 10.0.201
  • Host runtime: .NET 10.0.8
  • OS: Windows 10.0.26200
  • Architecture: x64
  • Reproduced in an isolated minimal repro
  • Also reproduced in a larger real application using the same package version

Other information

This looks like one of two possibilities:

  • the documented appsettings.json shape is incorrect, or
  • DataClassification binding is currently broken for AddExtendedHttpClientLogging(configurationSection)

This does not appear to be specific to Authorization; it also reproduces with the docs sample using only User-Agent and Content-Type.

Marcus-Kanon avatar Jun 09 '26 13:06 Marcus-Kanon