Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 1609c68

Browse files
authored
Xunit logger ignores exceptions from output helper (#592)
1 parent e4c731e commit 1609c68

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/Microsoft.Extensions.Logging.Testing/XunitLoggerProvider.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ public void Log<TState>(
5656
}
5757
var firstLinePrefix = $"| {_category} {logLevel}: ";
5858
var lines = formatter(state, exception).Split('\n');
59-
_output.WriteLine(firstLinePrefix + lines.First().TrimEnd(NewLineChars));
59+
WriteLine(firstLinePrefix + lines.First().TrimEnd(NewLineChars));
6060

6161
var additionalLinePrefix = "|" + new string(' ', firstLinePrefix.Length - 1);
6262
foreach (var line in lines.Skip(1))
6363
{
64-
_output.WriteLine(additionalLinePrefix + line.TrimEnd(NewLineChars));
64+
WriteLine(additionalLinePrefix + line.TrimEnd(NewLineChars));
6565
}
6666
}
6767

@@ -71,6 +71,21 @@ public bool IsEnabled(LogLevel logLevel)
7171
public IDisposable BeginScope<TState>(TState state)
7272
=> new NullScope();
7373

74+
private void WriteLine(string message)
75+
{
76+
try
77+
{
78+
_output.WriteLine(message);
79+
}
80+
catch (Exception)
81+
{
82+
// We could fail because we're on a background thread and our captured ITestOutputHelper is
83+
// busted (if the test "completed" before the background thread fired).
84+
// So, ignore this. There isn't really anything we can do but hope the
85+
// caller has additional loggers registered
86+
}
87+
}
88+
7489
private class NullScope : IDisposable
7590
{
7691
public void Dispose()

test/Microsoft.Extensions.Logging.Testing.Tests/XunitLoggerProviderTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,43 @@ public void LoggerProviderPrependsPrefixToEachLine()
6060
Assert.Equal(expectedOutput, testTestOutputHelper.Output);
6161
}
6262

63+
[Fact]
64+
public void LoggerProviderDoesNotThrowIfOutputHelperThrows()
65+
{
66+
var testTestOutputHelper = new TestTestOutputHelper();
67+
var loggerFactory = new LoggerFactory();
68+
loggerFactory.AddXunit(testTestOutputHelper);
69+
testTestOutputHelper.Throw = true;
70+
71+
var logger = loggerFactory.CreateLogger("TestCategory");
72+
logger.LogInformation("This is a" + Environment.NewLine + "multi-line" + Environment.NewLine + "message");
73+
74+
Assert.Equal(0, testTestOutputHelper.Output.Length);
75+
}
76+
6377
private class TestTestOutputHelper : ITestOutputHelper
6478
{
6579
private StringBuilder _output = new StringBuilder();
6680

81+
public bool Throw { get; set; }
82+
6783
public string Output => _output.ToString();
6884

6985
public void WriteLine(string message)
7086
{
87+
if (Throw)
88+
{
89+
throw new Exception("Boom!");
90+
}
7191
_output.AppendLine(message);
7292
}
7393

7494
public void WriteLine(string format, params object[] args)
7595
{
96+
if (Throw)
97+
{
98+
throw new Exception("Boom!");
99+
}
76100
_output.AppendLine(string.Format(format, args));
77101
}
78102
}

0 commit comments

Comments
 (0)