-
-
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.
fix: Differentiate message format for test output helper and message …
…sink Test output helper: include timestamp Message sink: don't include timestamp since it's already included by xUnit
- Loading branch information
Showing
7 changed files
with
68 additions
and
108 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 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,20 @@ | ||
namespace Testcontainers.Xunit; | ||
|
||
internal abstract class Logger : ILogger | ||
{ | ||
protected static string GetMessage<TState>(TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
{ | ||
return exception == null ? formatter(state, null) : $"{formatter(state, exception)}{Environment.NewLine}{exception}"; | ||
} | ||
|
||
protected abstract void Log<TState>(TState state, Exception exception, Func<TState, Exception, string> formatter); | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
{ | ||
Log(state, exception, formatter); | ||
} | ||
|
||
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None; | ||
|
||
public IDisposable BeginScope<TState>(TState state) => new NullScope(); | ||
} |
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,21 @@ | ||
namespace Testcontainers.Xunit; | ||
|
||
internal sealed class MessageSinkLogger(IMessageSink messageSink) : Logger | ||
{ | ||
protected override void Log<TState>(TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
{ | ||
if (messageSink == null) | ||
{ | ||
return; | ||
} | ||
|
||
var message = GetMessage(state, exception, formatter); | ||
messageSink.OnMessage(new DiagnosticMessage($"[testcontainers.org] {message}")); | ||
} | ||
|
||
/// <returns> | ||
/// The hash code of the underlying message sink, because <see cref="DotNet.Testcontainers.Clients.DockerApiClient.LogContainerRuntimeInfoAsync" /> | ||
/// logs the runtime information once per Docker Engine API client and logger. | ||
/// </returns> | ||
public override int GetHashCode() => messageSink?.GetHashCode() ?? 0; | ||
} |
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,8 @@ | ||
namespace Testcontainers.Xunit; | ||
|
||
internal sealed class NullScope : IDisposable | ||
{ | ||
public void Dispose() | ||
{ | ||
} | ||
} |
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,17 @@ | ||
namespace Testcontainers.Xunit; | ||
|
||
internal sealed class TestOutputLogger(ITestOutputHelper testOutputHelper) : Logger | ||
{ | ||
private readonly Stopwatch _stopwatch = Stopwatch.StartNew(); | ||
|
||
protected override void Log<TState>(TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
{ | ||
if (testOutputHelper == null) | ||
{ | ||
return; | ||
} | ||
|
||
var message = GetMessage(state, exception, formatter); | ||
testOutputHelper.WriteLine($@"[testcontainers.org {_stopwatch.Elapsed:hh\:mm\:ss\.fff}] {message}"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.