Skip to content

Add configurable HTTP status code log levels for HttpClientLogging#7638

Open
Marcus-Kanon wants to merge 4 commits into
dotnet:mainfrom
Marcus-Kanon:feature/7637-configurable-http-logging-level
Open

Add configurable HTTP status code log levels for HttpClientLogging#7638
Marcus-Kanon wants to merge 4 commits into
dotnet:mainfrom
Marcus-Kanon:feature/7637-configurable-http-logging-level

Conversation

@Marcus-Kanon

@Marcus-Kanon Marcus-Kanon commented Jul 17, 2026

Copy link
Copy Markdown

Summary

Adds StatusCodeLogLevelRules and ExceptionLogLevel properties to LoggingOptions, allowing users to map HTTP status codes (or ranges) to specific log levels with first-match-wins evaluation.

Motivation

Currently, AddExtendedHttpClientLogging hard-codes log levels: 400-599 → Error, everything else → Information. In microservice environments, some 4xx codes (e.g. 404 for cache misses, 409 for optimistic concurrency) are expected and not errors. This feature allows callers to control the log level per status code range.

Changes

  • New class: HttpStatusCodeLogLevelRule — maps a status code or range to a LogLevel, with IValidatableObject cross-property validation
  • LoggingOptions: Added StatusCodeLogLevelRules (list of rules, first-match-wins) and ExceptionLogLevel (defaults to Error)
  • HttpClientLogger: GetLogLevel is now an instance method that evaluates rules before falling back to the built-in 400-599 → Error default
  • Log.OutgoingRequestError: Now accepts a LogLevel parameter instead of hard-coding Error
  • All new APIs marked [Experimental] per repo conventions

Usage

services.AddHttpClient(MyClient)
    .AddExtendedHttpClientLogging(options =>
    {
        options.StatusCodeLogLevelRules =
        [
            new() { FromStatusCode = 404, LogLevel = LogLevel.Information },
            new() { FromStatusCode = 400, ToStatusCode = 499, LogLevel = LogLevel.Warning },
            new() { FromStatusCode = 500, ToStatusCode = 599, LogLevel = LogLevel.Critical },
        ];
        options.ExceptionLogLevel = LogLevel.Critical;
    });

Tests

  • HttpStatusCodeLogLevelRuleTest — validation tests (valid ranges, invalid ranges, boundary conditions)
  • HttpClientLoggerStatusCodeLogLevelTest — behavior tests (rule matching, first-match-wins, fallback, exception log level)

Fixes #7637

Add StatusCodeLogLevelRules and ExceptionLogLevel properties to
LoggingOptions, allowing users to map HTTP status codes or ranges
to specific log levels with first-match-wins evaluation.

Fixes dotnet#7637
Copilot AI review requested due to automatic review settings July 17, 2026 09:53
@Marcus-Kanon
Marcus-Kanon requested a review from a team as a code owner July 17, 2026 09:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends Microsoft.Extensions.Http.Logging (within Microsoft.Extensions.Http.Diagnostics) to let callers configure log levels based on HTTP response status codes (and configure exception log level), addressing scenarios where some 4xx responses are expected and should not be logged as Error.

Changes:

  • Introduces HttpStatusCodeLogLevelRule with validation for status-code ranges.
  • Adds StatusCodeLogLevelRules and ExceptionLogLevel to LoggingOptions, enabling first-match-wins log-level mapping.
  • Updates HttpClientLogger/Log to apply configured log levels and adds unit tests covering rule matching and validation.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/Libraries/Microsoft.Extensions.Http.Diagnostics.Tests/Logging/HttpStatusCodeLogLevelRuleTest.cs Adds validation-focused tests for rule range correctness and boundary cases.
test/Libraries/Microsoft.Extensions.Http.Diagnostics.Tests/Logging/HttpClientLoggerStatusCodeLogLevelTest.cs Adds behavior tests for rule matching, ordering (first-match-wins), fallback behavior, and exception log level.
src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/LoggingOptions.cs Adds new options for status-code-to-log-level mapping and configurable exception log level.
src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/Log.cs Makes outgoing request error logging accept a dynamic LogLevel.
src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/HttpClientLogger.cs Implements rule evaluation and uses configured exception log level.
src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/HttpStatusCodeLogLevelRule.cs Adds the rule type that maps status code ranges to LogLevel with cross-property validation.

Marcus Kanon (consultant) added 3 commits July 17, 2026 12:08
Treat null as empty rules list to prevent NRE when property
is set to null via configuration binding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Configurable log level for HTTP response status codes in AddExtendedHttpClientLogging

2 participants