Skip to content

Commit 7564073

Browse files
Copilotstephentoub
andcommitted
Fix test failure from background logging after xUnit test disposal
Add try-catch in XunitLoggerProvider to gracefully handle exceptions when logging after xUnit's TestOutputHelper has been disposed. This prevents NullReferenceException when background work continues after test completion. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent e8e5afd commit 7564073

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

tests/Common/Utils/XunitLoggerProvider.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ public void Log<TState>(
3434
sb.Append(exception.ToString());
3535
}
3636

37-
output.WriteLine(sb.ToString());
37+
try
38+
{
39+
output.WriteLine(sb.ToString());
40+
}
41+
catch
42+
{
43+
// Ignore exceptions from xUnit's TestOutputHelper when the test has already completed.
44+
// Background work may continue logging after xUnit has disposed the test context.
45+
}
3846
}
3947

4048
public bool IsEnabled(LogLevel logLevel) => true;

0 commit comments

Comments
 (0)