Skip to content

Register DI prerequisites in AspNetCore.Diagnostics.Middleware extension methods - #7859

Merged
evgenyfedorov2 merged 4 commits into
dotnet:mainfrom
evgenyfedorov2:evgenyfedorov2-fix-middleware-di-prereqs
Oct 20, 2026
Merged

evgenyfedorov2 merged 4 commits into
dotnet:mainfrom
evgenyfedorov2:evgenyfedorov2-fix-middleware-di-prereqs

Conversation

@evgenyfedorov2

@evgenyfedorov2 evgenyfedorov2 commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Fixes #4684

  • AddHttpLoggingRedaction() now calls AddRedaction(). AddHttpLogEnricher<T>() and AddHttpServerLatencyTelemetry() call AddHttpLoggingRedaction(), so they register it too. Without further configuration, data classified as anything other than DataClassification.None is erased.
  • AddRequestLatencyTelemetry() and AddRequestCheckpoint() now call AddLatencyContext().
  • AddRequestCheckpoint() also registers the per-request ILatencyContext, using the same factory as AddRequestLatencyTelemetry(), because the CapturePipelineEntryMiddleware it adds resolves ILatencyContext on every request. AddRequestCheckpoint() without AddRequestLatencyTelemetry() previously failed at startup and now works.

All use TryAdd, so implementations registered earlier are kept. As a result, AddFakeRedaction() or a custom IRedactorProvider must be registered before AddHttpLoggingRedaction(); AddRedaction(...) can be called before or after. The README documents this and drops the now-redundant AddLatencyContext() call from the sample.

Microsoft.AspNetCore.Diagnostics.Middleware now references Microsoft.Extensions.Compliance.Redaction.

Same approach as #7854 (#4675) for Microsoft.Extensions.Http.Diagnostics.

Microsoft Reviewers: Open in CodeFlow

…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>
Copilot AI balanced review requested due to automatic review settings October 19, 2026 07:41
@evgenyfedorov2
evgenyfedorov2 requested a review from a team as a code owner October 19, 2026 07:41
@evgenyfedorov2 evgenyfedorov2 self-assigned this Oct 19, 2026

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.

🟡 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

evgenyfedorov2 and others added 3 commits October 19, 2026 10:11
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>
@dotnet-comment-bot

Copy link
Copy Markdown
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Extensions.Diagnostics.Testing Line 99 98.65 🔻
Microsoft.Extensions.Telemetry Line 93 92.75 🔻
Microsoft.Extensions.AI Line 89 88.4 🔻
Microsoft.Extensions.AI.OpenAI Line 75 68.24 🔻
Microsoft.Extensions.AI.OpenAI Branch 75 57.54 🔻
Microsoft.Extensions.DataIngestion.MarkItDown Line 75 4.46 🔻
Microsoft.Extensions.DataIngestion.MarkItDown Branch 75 0 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring Line 99 96.07 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring Branch 99 92.76 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes Line 99 97.73 🔻
Microsoft.Extensions.ServiceDiscovery.Dns Line 75 71.61 🔻
Microsoft.Extensions.ServiceDiscovery Line 75 68.72 🔻
Microsoft.Extensions.ServiceDiscovery Branch 75 71.43 🔻
Microsoft.Extensions.ServiceDiscovery.Abstractions Line 75 42.11 🔻
Microsoft.Extensions.ServiceDiscovery.Abstractions Branch 75 42.86 🔻
Microsoft.Extensions.ServiceDiscovery.Yarp Line 75 73.85 🔻
Microsoft.Extensions.ServiceDiscovery.Yarp Branch 75 70 🔻
Microsoft.Extensions.VectorData.Abstractions Line 75 37.39 🔻
Microsoft.Extensions.VectorData.Abstractions Branch 75 22.73 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Extensions.Http.Diagnostics 94 95
Microsoft.Gen.BuildMetadata 97 100
Microsoft.Gen.MetadataExtractor 57 73
Microsoft.Gen.MetricsReports 67 69
Microsoft.Extensions.AI.Abstractions 82 86
Microsoft.Extensions.AI.Evaluation.NLP 0 78
Microsoft.Extensions.Caching.Hybrid 82 85
Microsoft.Extensions.DataIngestion 75 89
Microsoft.Extensions.DataIngestion.Markdig 75 90
Microsoft.Extensions.Http.Resilience 97 100

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1640094&view=codecoverage-tab

@evgenyfedorov2
evgenyfedorov2 merged commit 7d52f46 into dotnet:main Oct 20, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve discoverability of DI prerequisites in AspNetCore.Diagnostics.Middleware package

4 participants