Add configurable HTTP status code log levels for HttpClientLogging#7638
Open
Marcus-Kanon wants to merge 4 commits into
Open
Add configurable HTTP status code log levels for HttpClientLogging#7638Marcus-Kanon wants to merge 4 commits into
Marcus-Kanon wants to merge 4 commits into
Conversation
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
Contributor
There was a problem hiding this comment.
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
HttpStatusCodeLogLevelRulewith validation for status-code ranges. - Adds
StatusCodeLogLevelRulesandExceptionLogLeveltoLoggingOptions, enabling first-match-wins log-level mapping. - Updates
HttpClientLogger/Logto 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. |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
StatusCodeLogLevelRulesandExceptionLogLevelproperties toLoggingOptions, allowing users to map HTTP status codes (or ranges) to specific log levels with first-match-wins evaluation.Motivation
Currently,
AddExtendedHttpClientLogginghard-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
HttpStatusCodeLogLevelRule— maps a status code or range to aLogLevel, withIValidatableObjectcross-property validationLoggingOptions: AddedStatusCodeLogLevelRules(list of rules, first-match-wins) andExceptionLogLevel(defaults to Error)HttpClientLogger:GetLogLevelis now an instance method that evaluates rules before falling back to the built-in 400-599 → Error defaultLog.OutgoingRequestError: Now accepts aLogLevelparameter instead of hard-coding Error[Experimental]per repo conventionsUsage
Tests
HttpStatusCodeLogLevelRuleTest— validation tests (valid ranges, invalid ranges, boundary conditions)HttpClientLoggerStatusCodeLogLevelTest— behavior tests (rule matching, first-match-wins, fallback, exception log level)Fixes #7637