Fix flaky HttpRequestBuffering_DoesNotBufferDisabledOrOversizedLogs test#7304
Open
Fix flaky HttpRequestBuffering_DoesNotBufferDisabledOrOversizedLogs test#7304
Conversation
…ogs by suppressing ASP.NET Core infrastructure logs Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix flaky acceptance test for HTTP request buffering
Fix flaky HttpRequestBuffering_DoesNotBufferDisabledOrOversizedLogs test
Feb 14, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a flaky test HttpRequestBuffering_DoesNotBufferDisabledOrOversizedLogs that was intermittently failing with "Expected: 6, Actual: 7" due to non-deterministic ASP.NET Core infrastructure logs (DfaMatcher, HttpLoggingMiddleware) from multiple HTTP requests. The test validates that disabled-level and oversized logs bypass the request buffering mechanism.
Changes:
- Replaced selective DfaMatcher logging with complete suppression of all
Microsoft.AspNetCoreinfrastructure logs - Added explicit
Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddlewarefilter to override base configuration priority - Updated expected log count from 6 to 5 to reflect only application logs under test
test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Logging/AcceptanceTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
auto-merge was automatically disabled
February 19, 2026 20:56
Head branch was pushed to by a user without write access
stephentoub
approved these changes
Feb 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.
Fix for Flaky Test: HttpRequestBuffering_DoesNotBufferDisabledOrOversizedLogs
Summary
Fixed the flaky test by suppressing ASP.NET Core infrastructure logs (DfaMatcher and HttpLoggingMiddleware) that were non-deterministically appearing in the log output.
Changes Made
Microsoft.AspNetCore.HttpLoggingfilter with broaderMicrosoft.AspNetCorefilter at LogLevel.NoneMicrosoft.AspNetCore.HttpLogging.HttpLoggingMiddlewarefilter to override base configurationRoot Cause
The test makes two HTTP requests that each trigger ASP.NET Core routing logs (DfaMatcher) and HTTP logging middleware logs. The non-determinism arose from:
Solution
By suppressing ALL Microsoft.AspNetCore infrastructure logs (both DfaMatcher and HttpLoggingMiddleware), the test now only captures the expected application logs:
Validation
Original prompt
Problem
The test
Microsoft.AspNetCore.Diagnostics.Logging.Test.AcceptanceTests.HttpRequestBuffering_DoesNotBufferDisabledOrOversizedLogsis flaky in CI, failing with:at line 950 in
test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Logging/AcceptanceTests.cs.Root Cause
The test makes two HTTP requests (
/flushalllogsand/logatrequest). Each request goes through ASP.NET Core routing, which triggersDfaMatcherDebug-level logs. The test has a filter enablingMicrosoft.AspNetCore.Routing.Matching.DfaMatcheratLogLevel.Debug, but only accounts for one DfaMatcher log in its expected count of 6.The non-determinism arises from the interaction between per-request buffering and global buffering with
AutoFlushDuration = TimeSpan.Zero:/flushalllogs) buffers a DfaMatcher Debug log in its per-request buffer, then flushes both the per-request and global buffers. This sets_lastFlushTimestampon the globalGlobalBufferfor the DfaMatcher category./logatrequest) also produces a DfaMatcher Debug log. This log goes to a new per-requestIncomingRequestLogBuffer(fresh scope,_lastFlushTimestamp = default), so it gets buffered there. Since/logatrequestnever callsFlush(), the log is silently dropped when the scope disposes → 6 total logs.HttpContextis briefly null during request startup), the global buffer'sTryEnqueuereturnsfalse(because_lastFlushTimestampwas already set andAutoFlushDuration = ZeromeansGetUtcNow() >= _lastFlushTimestamp), so the log is emitted directly → 7 total logs.Options Considered
Option A: Assert on ≥ 6 and filter by category — Rather than asserting an exact total count, assert on the specific logs by category (e.g., 3 from "test", 2 from "logatrequest"). This validates the actual buffering behavior without being sensitive to infrastructure log counts. However, it weakens the test by not catching unexpected extra logs.
Option B: Use a dedicated test logger instead of DfaMatcher — Replace the DfaMatcher dependency with explicit test loggers. The DfaMatcher logs aren't what this test is testing anyway.
Option C: Assert on exact count and filter by category — Similar to Option A but still validates that no unexpected logs appear in the categories under test.
Option D: Disable DfaMatcher logs entirely — Since this test's purpose is to verify that disabled-level and oversized logs bypass the buffer (not to test DfaMatcher buffering), suppress all
Microsoft.AspNetCorelogs by changing the DfaMatcher filter fromLogLevel.Debugto removing it and instead settingMicrosoft.AspNetCoretoLogLevel.None. This eliminates the infrastructure noise entirely and makes the exact log count deterministic.Chosen Direction: Option D
Option D is the cleanest fix. The test's purpose is to verify that disabled-level logs and oversized logs bypass the buffer — it doesn't need DfaMatcher logs to prove that. The other buffering tests (
HttpRequestBufferingandHttpRequestBuffering_RespectsAutoFlush) that actually need DfaMatcher logs only make one flushing request, so they avoid the two-request problem.Required Changes
In
test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Logging/AcceptanceTests.cs, in theHttpRequestBuffering_DoesNotBufferDisabledOrOversizedLogstest method (starting around line 895):Remove the line
builder.AddFilter("Microsoft.AspNetCore.Routing.Matching.DfaMatcher", LogLevel.Debug);(line 906). The existing filterbuilder.AddFilter("Microsoft.AspNetCore.HttpLogging", LogLevel.None)at line 910 should be kept, but it's no longer the only Microsoft.AspNetCore filter needed.Change the
builder.AddFilter("Microsoft.AspNetCore.HttpLogging", LogLevel.None)line to suppress ALL ASP.NET Core infrastructure logs:builder.AddFilter("Microsoft.AspNetCore", LogLevel.None). This covers both HttpLogging and DfaMatcher and any other ASP.NET Core infrastructure logs. Note: in theRunAsynchelper method (lines 152-177), there are already filters like.AddFilter("Microsoft.AspNetCore", LogLevel.Warning)set on the logging builder, but the test'sconfiguredelegate callsbuilder.AddLogging(...)which adds additional filters. The new filterMicrosoft.AspNetCoreatLogLevel.Nonein the test's ownAddLoggingcall should take precedence for the more specific category.Update the assertion on line 950 from
Assert.Equal(6, logCollector.Count)toAssert.Equal(5, logCollector.Count)— since we now expect:This pull request was created from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Microsoft Reviewers: Open in CodeFlow