This repository was archived by the owner on Dec 13, 2018. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +41
-2
lines changed
src/Microsoft.Extensions.Logging.Testing
test/Microsoft.Extensions.Logging.Testing.Tests Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Original file line number Diff line number Diff line change @@ -56,12 +56,12 @@ public void Log<TState>(
56
56
}
57
57
var firstLinePrefix = $ "| { _category } { logLevel } : ";
58
58
var lines = formatter ( state , exception ) . Split ( '\n ' ) ;
59
- _output . WriteLine ( firstLinePrefix + lines . First ( ) . TrimEnd ( NewLineChars ) ) ;
59
+ WriteLine ( firstLinePrefix + lines . First ( ) . TrimEnd ( NewLineChars ) ) ;
60
60
61
61
var additionalLinePrefix = "|" + new string ( ' ' , firstLinePrefix . Length - 1 ) ;
62
62
foreach ( var line in lines . Skip ( 1 ) )
63
63
{
64
- _output . WriteLine ( additionalLinePrefix + line . TrimEnd ( NewLineChars ) ) ;
64
+ WriteLine ( additionalLinePrefix + line . TrimEnd ( NewLineChars ) ) ;
65
65
}
66
66
}
67
67
@@ -71,6 +71,21 @@ public bool IsEnabled(LogLevel logLevel)
71
71
public IDisposable BeginScope < TState > ( TState state )
72
72
=> new NullScope ( ) ;
73
73
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
+
74
89
private class NullScope : IDisposable
75
90
{
76
91
public void Dispose ( )
Original file line number Diff line number Diff line change @@ -60,19 +60,43 @@ public void LoggerProviderPrependsPrefixToEachLine()
60
60
Assert . Equal ( expectedOutput , testTestOutputHelper . Output ) ;
61
61
}
62
62
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
+
63
77
private class TestTestOutputHelper : ITestOutputHelper
64
78
{
65
79
private StringBuilder _output = new StringBuilder ( ) ;
66
80
81
+ public bool Throw { get ; set ; }
82
+
67
83
public string Output => _output . ToString ( ) ;
68
84
69
85
public void WriteLine ( string message )
70
86
{
87
+ if ( Throw )
88
+ {
89
+ throw new Exception ( "Boom!" ) ;
90
+ }
71
91
_output . AppendLine ( message ) ;
72
92
}
73
93
74
94
public void WriteLine ( string format , params object [ ] args )
75
95
{
96
+ if ( Throw )
97
+ {
98
+ throw new Exception ( "Boom!" ) ;
99
+ }
76
100
_output . AppendLine ( string . Format ( format , args ) ) ;
77
101
}
78
102
}
You can’t perform that action at this time.
0 commit comments