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:
parent
5084a79068
commit
50d6d0b570
1 changed files with 14 additions and 1 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue