diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/HttpStatusCodeLogLevelRule.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/HttpStatusCodeLogLevelRule.cs
new file mode 100644
index 00000000000..58d26ae54ab
--- /dev/null
+++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/HttpStatusCodeLogLevelRule.cs
@@ -0,0 +1,46 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Diagnostics.CodeAnalysis;
+using Microsoft.Extensions.Logging;
+using Microsoft.Shared.DiagnosticIds;
+
+namespace Microsoft.Extensions.Http.Logging;
+
+///
+/// Maps a status code or range of status codes to a specific log level.
+///
+[Experimental(diagnosticId: DiagnosticIds.Experiments.Telemetry, UrlFormat = DiagnosticIds.UrlFormat)]
+public class HttpStatusCodeLogLevelRule : IValidatableObject
+{
+ ///
+ /// Gets or sets the minimum status code this rule applies to (inclusive).
+ ///
+ [Range(100, 599)]
+ public int FromStatusCode { get; set; }
+
+ ///
+ /// Gets or sets the maximum status code this rule applies to (inclusive).
+ /// When , matches only (exact match).
+ ///
+ [Range(100, 599)]
+ public int? ToStatusCode { get; set; }
+
+ ///
+ /// Gets or sets the log level to use for responses matching this rule.
+ ///
+ public LogLevel LogLevel { get; set; } = LogLevel.Information;
+
+ ///
+ public IEnumerable Validate(ValidationContext validationContext)
+ {
+ if (ToStatusCode.HasValue && ToStatusCode.Value < FromStatusCode)
+ {
+ yield return new ValidationResult(
+ $"{nameof(ToStatusCode)} must be greater than or equal to {nameof(FromStatusCode)}.",
+ [nameof(ToStatusCode)]);
+ }
+ }
+}
diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/HttpClientLogger.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/HttpClientLogger.cs
index 536c08eee6d..a3a247d8186 100644
--- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/HttpClientLogger.cs
+++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/HttpClientLogger.cs
@@ -31,6 +31,8 @@ internal sealed class HttpClientLogger : IHttpClientAsyncLogger
private readonly bool _logResponseHeaders;
private readonly bool _logRequestHeaders;
private readonly bool _pathParametersRedactionSkipped;
+ private readonly IReadOnlyList _statusCodeLogLevelRules;
+ private readonly LogLevel _exceptionLogLevel;
private ILogger _logger;
private IHttpRequestReader _httpRequestReader;
private IHttpClientLogEnricher[] _enrichers;
@@ -62,6 +64,10 @@ internal HttpClientLogger(
_logResponseHeaders = options.ResponseHeadersDataClasses.Count > 0;
_logRequestHeaders = options.RequestHeadersDataClasses.Count > 0;
_pathParametersRedactionSkipped = options.RequestPathParameterRedactionMode == HttpRouteParameterRedactionMode.None;
+ _statusCodeLogLevelRules = options.StatusCodeLogLevelRules is { } rules
+ ? rules as IReadOnlyList ?? rules.ToArray()
+ : [];
+ _exceptionLogLevel = options.ExceptionLogLevel;
}
public async ValueTask