Skip to content

Console.Write where ExecutionContext flow is suppressed is never written #5120

@JoostK

Description

@JoostK

NUnit tracks the current test using an AsyncLocal in TestExecutionContext, but async state propagation does not work when System.Threading.ExecutionContext flowing is suppressed, causing the test context to be unavailable. In this scenario, using Console.Write{,Line}(...) gets rerouted through TextCapture into an ad-hoc TestContext (AdhocContext) of which the output is never written anywhere. TextCapture's aim is to write such out-of-band writes into its _defaultWriter, but the implicit availability of AdhocContext since #2279 has made those fallback branches dead code.

This problem surfaced when using ASP.NET's TestServer, which by-default handles requests using ThreadPool.UnsafeQueueUserWorkItem, which doesn't propagate the current ExecutionContext (unlike ThreadPool.QueueUserWorkItem). The test context can be propagated into the ASP.NET request processing by enabling TestServerOptions.PreserveExecutionContext, but it took me a while to find this option when debugging to figure out where logging got lost.

I'll be opening a PR shortly with a fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions