-
-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2151a7
commit 630a073
Showing
7 changed files
with
97 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
namespace Testcontainers.Xunit; | ||
|
||
internal sealed class XunitLoggerProvider : ILoggerProvider | ||
{ | ||
private readonly Stopwatch _stopwatch = Stopwatch.StartNew(); | ||
|
||
private readonly ITestOutputHelper _testOutputHelper; | ||
|
||
public XunitLoggerProvider(IMessageSink messageSink) | ||
{ | ||
_testOutputHelper = new MessageSinkTestOutputHelper(messageSink); | ||
} | ||
|
||
public XunitLoggerProvider(ITestOutputHelper testOutputHelper) | ||
{ | ||
_testOutputHelper = testOutputHelper; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
return new XunitLogger(_stopwatch, _testOutputHelper, categoryName); | ||
} | ||
|
||
private sealed class MessageSinkTestOutputHelper : ITestOutputHelper | ||
{ | ||
private readonly IMessageSink _messageSink; | ||
|
||
#if XUNIT_V3 | ||
public string Output => throw new NotImplementedException(); | ||
#endif | ||
|
||
public MessageSinkTestOutputHelper(IMessageSink messageSink) | ||
{ | ||
_messageSink = messageSink; | ||
} | ||
|
||
public void WriteLine(string message) | ||
{ | ||
_messageSink.OnMessage(new DiagnosticMessage(message)); | ||
} | ||
|
||
public void WriteLine(string format, params object[] args) | ||
{ | ||
_messageSink.OnMessage(new DiagnosticMessage(format, args)); | ||
} | ||
} | ||
|
||
private sealed class XunitLogger : ILogger | ||
{ | ||
private readonly Stopwatch _stopwatch; | ||
|
||
private readonly ITestOutputHelper _testOutputHelper; | ||
|
||
private readonly string _categoryName; | ||
|
||
public XunitLogger(Stopwatch stopwatch, ITestOutputHelper testOutputHelper, string categoryName) | ||
{ | ||
_stopwatch = stopwatch; | ||
_testOutputHelper = testOutputHelper; | ||
_categoryName = categoryName; | ||
} | ||
|
||
public IDisposable BeginScope<TState>(TState state) | ||
{ | ||
return Disposable.Instance; | ||
} | ||
|
||
public bool IsEnabled(LogLevel logLevel) | ||
{ | ||
return logLevel != LogLevel.None; | ||
} | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
{ | ||
_testOutputHelper.WriteLine("[{0} {1:hh\\:mm\\:ss\\.ff}] {2}", _categoryName, _stopwatch.Elapsed, formatter.Invoke(state, exception)); | ||
} | ||
|
||
private sealed class Disposable : IDisposable | ||
{ | ||
private Disposable() | ||
{ | ||
} | ||
|
||
public static IDisposable Instance { get; } = new Disposable(); | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
} | ||
} | ||
} |