Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal sealed class HttpLoggingRedactionInterceptor : IHttpLoggingInterceptor
private readonly HeaderReader _responseHeadersReader;
private readonly string[] _excludePathStartsWith;
private readonly FrozenDictionary<string, DataClassification> _parametersToRedactMap;
private readonly bool _reportUnmatchedRoutes;

public HttpLoggingRedactionInterceptor(
IOptions<LoggingRedactionOptions> options,
Expand All @@ -59,6 +60,7 @@ public HttpLoggingRedactionInterceptor(
_responseHeadersReader = new(optionsValue.ResponseHeadersDataClasses, redactorProvider, HttpLoggingTagNames.ResponseHeaderPrefix);

_excludePathStartsWith = optionsValue.ExcludePathStartsWith.ToArray();
_reportUnmatchedRoutes = optionsValue.ReportUnmatchedRoutes;
}

public ValueTask OnRequestAsync(HttpLoggingInterceptorContext logContext)
Expand Down Expand Up @@ -115,6 +117,10 @@ public ValueTask OnRequestAsync(HttpLoggingInterceptorContext logContext)
}
}
}
else if (_reportUnmatchedRoutes)
{
path = context.Request.Path.ToString();
}
}
else if (request.Path.HasValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
#pragma warning disable CA2227 // Collection properties should be read only
public ISet<string> ExcludePathStartsWith { get; set; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
#pragma warning restore CA2227 // Collection properties should be read only

/// <summary>
/// Gets or sets a value indicating whether to report unmatched routes.
/// </summary>
public bool ReportUnmatchedRoutes { get; set; } = false;

Check failure on line 107 in src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/LoggingRedactionOptions.cs

View check run for this annotation

Azure Pipelines / extensions-ci (Correctness WarningsCheck)

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/LoggingRedactionOptions.cs#L107

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/LoggingRedactionOptions.cs(107,53): error CA1805: (NETCORE_ENGINEERING_TELEMETRY=Build) Member 'ReportUnmatchedRoutes' is explicitly initialized to its default value (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1805)

Check failure on line 107 in src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/LoggingRedactionOptions.cs

View check run for this annotation

Azure Pipelines / extensions-ci (Correctness WarningsCheck)

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/LoggingRedactionOptions.cs#L107

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/LoggingRedactionOptions.cs(107,53): error CA1805: (NETCORE_ENGINEERING_TELEMETRY=Build) Member 'ReportUnmatchedRoutes' is explicitly initialized to its default value (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1805)

Check failure on line 107 in src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/LoggingRedactionOptions.cs

View check run for this annotation

Azure Pipelines / extensions-ci

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/LoggingRedactionOptions.cs#L107

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/LoggingRedactionOptions.cs(107,53): error CA1805: (NETCORE_ENGINEERING_TELEMETRY=Build) Member 'ReportUnmatchedRoutes' is explicitly initialized to its default value (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1805)

Check failure on line 107 in src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/LoggingRedactionOptions.cs

View check run for this annotation

Azure Pipelines / extensions-ci

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/LoggingRedactionOptions.cs#L107

src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/LoggingRedactionOptions.cs(107,53): error CA1805: (NETCORE_ENGINEERING_TELEMETRY=Build) Member 'ReportUnmatchedRoutes' is explicitly initialized to its default value (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1805)
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ await RunAsync(
x.MediaTypeOptions.Clear();
x.MediaTypeOptions.AddText("text/*");
x.LoggingFields |= HttpLoggingFields.RequestBody;
}).AddHttpLoggingRedaction(),
}).AddHttpLoggingRedaction(options => options.ReportUnmatchedRoutes = true),
async (logCollector, client) =>
{
const string Content = "Client: hello!";

using var content = new StringContent(Content, null, requestContentType);
using var response = await client.PostAsync("/", content).ConfigureAwait(false);
using var response = await client.PostAsync("/myroute/123", content).ConfigureAwait(false);
Assert.True(response.IsSuccessStatusCode);

await WaitForLogRecordsAsync(logCollector, _defaultLogTimeout);
Expand All @@ -247,7 +247,7 @@ await RunAsync(
Assert.DoesNotContain(state, x => x.Key.StartsWith(HttpLoggingTagNames.RequestHeaderPrefix));
Assert.DoesNotContain(state, x => x.Key.StartsWith(HttpLoggingTagNames.ResponseHeaderPrefix));
Assert.Single(state, x => x.Key == HttpLoggingTagNames.Host && !string.IsNullOrEmpty(x.Value));
Assert.Single(state, x => x.Key == HttpLoggingTagNames.Path && x.Value == TelemetryConstants.Unknown);
Assert.Single(state, x => x.Key == HttpLoggingTagNames.Path && x.Value == "/myroute/123");
Assert.Single(state, x => x.Key == HttpLoggingTagNames.StatusCode && x.Value == responseStatus);
Assert.Single(state, x => x.Key == HttpLoggingTagNames.Method && x.Value == HttpMethod.Post.ToString());
Assert.Single(state, x => x.Key == HttpLoggingTagNames.Duration &&
Expand Down
Loading