Repository navigation
Register DI prerequisites in AspNetCore.Diagnostics.Middleware extension methods - #7859
Merged
evgenyfedorov2 merged 4 commits intoOct 20, 2026
Conversation
…ion methods AddHttpLoggingRedaction() now calls AddRedaction(); AddHttpLogEnricher<T>() and AddHttpServerLatencyTelemetry() go through it. AddRequestLatencyTelemetry() and AddRequestCheckpoint() now call AddLatencyContext(). All use TryAdd, so implementations registered earlier are kept. Fixes dotnet#4684 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Checkpoint-only registration now permits startup but leaves its middleware throwing during requests because the scoped latency context is missing.
1 open issue
What changed in this PR
Makes ASP.NET Core diagnostics setup simpler by registering redaction and latency prerequisites automatically, addressing #4684.
Changes:
- Registers default redaction and latency services while preserving existing registrations.
- Updates setup documentation and adds the redaction project dependency.
- Adds prerequisite-registration and request-level acceptance tests.
| File | Description |
|---|---|
| test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Logging/AcceptanceTests.Prerequisites.cs | Tests logging, redaction, and latency prerequisites. |
| test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Logging/AcceptanceTests.cs | Allows tests to disable fake redaction. |
| test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Latency/RequestLatencyTelemetryExtensionsTests.cs | Tests default and custom latency registrations. |
| test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Latency/RequestCheckpointExtensionsTests.cs | Tests checkpoint prerequisite registrations. |
| src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/README.md | Documents automatic registration and provider ordering. |
| src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Microsoft.AspNetCore.Diagnostics.Middleware.csproj | Adds the redaction dependency. |
| src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Logging/HttpLoggingServiceCollectionExtensions.cs | Registers default redaction services. |
| src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Latency/RequestLatencyTelemetryServiceCollectionExtensions.cs | Registers latency context prerequisites. |
| src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Latency/HttpLoggingServiceCollectionExtensions.cs | Removes a redundant prerequisite from API documentation. |
Review effort: Balanced
AddRequestCheckpoint() adds CapturePipelineEntryMiddleware, which resolves ILatencyContext for every request, but only AddRequestLatencyTelemetry() registered it. AddRequestCheckpoint() now registers it with TryAddScoped, using the same factory as AddRequestLatencyTelemetry(). Replace the end-to-end prerequisite tests with DI tests that assert the registered redactor and latency context. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Collaborator
🎉 Good job! The coverage increased 🎉
Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1640094&view=codecoverage-tab |
dariusclay
approved these changes
Oct 19, 2026
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.

Fixes #4684
AddHttpLoggingRedaction()now callsAddRedaction().AddHttpLogEnricher<T>()andAddHttpServerLatencyTelemetry()callAddHttpLoggingRedaction(), so they register it too. Without further configuration, data classified as anything other thanDataClassification.Noneis erased.AddRequestLatencyTelemetry()andAddRequestCheckpoint()now callAddLatencyContext().AddRequestCheckpoint()also registers the per-requestILatencyContext, using the same factory asAddRequestLatencyTelemetry(), because theCapturePipelineEntryMiddlewareit adds resolvesILatencyContexton every request.AddRequestCheckpoint()withoutAddRequestLatencyTelemetry()previously failed at startup and now works.All use
TryAdd, so implementations registered earlier are kept. As a result,AddFakeRedaction()or a customIRedactorProvidermust be registered beforeAddHttpLoggingRedaction();AddRedaction(...)can be called before or after. The README documents this and drops the now-redundantAddLatencyContext()call from the sample.Microsoft.AspNetCore.Diagnostics.Middlewarenow referencesMicrosoft.Extensions.Compliance.Redaction.Same approach as #7854 (#4675) for
Microsoft.Extensions.Http.Diagnostics.Microsoft Reviewers: Open in CodeFlow