Fix XunitLoggerProvider crash when background work logs after test disposal (#1229)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
This commit is contained in:
Copilot 2026-01-30 14:56:12 -08:00 • committed by GitHub
commit 50d6d0b570
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -34,7 +34,20 @@ public class XunitLoggerProvider(ITestOutputHelper output) : ILoggerProvider
sb.Append(exception.ToString());
}
output.WriteLine(sb.ToString());
try
{
output.WriteLine(sb.ToString());
}
catch (InvalidOperationException)
{
// Ignore exceptions from xUnit's TestOutputHelper when the test has already completed.
// Background work may continue logging after xUnit has disposed the test context.
}
catch (NullReferenceException)
{
// xUnit v3 may throw NullReferenceException in TestOutputHelper.QueueTestOutput()
// when the internal queue has been torn down after test completion.
}
}
public bool IsEnabled(LogLevel logLevel) => true;