Skip to content

Commit 043f18a

Browse files
committed
use custom sync console logger for MSTest
to work around microsoft/testfx#6457
1 parent acb94ea commit 043f18a

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#nullable enable
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace Renci.SshNet.IntegrationTests.Logging
5+
{
6+
internal class TestConsoleLogger(string categoryName) : ILogger
7+
{
8+
public IDisposable? BeginScope<TState>(TState state)
9+
where TState : notnull
10+
{
11+
return null;
12+
}
13+
14+
public bool IsEnabled(LogLevel logLevel)
15+
{
16+
return true;
17+
}
18+
19+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
20+
{
21+
StringBuilder sb = new StringBuilder();
22+
sb.Append(logLevel);
23+
sb.Append(": ");
24+
sb.Append(categoryName);
25+
sb.Append(": ");
26+
27+
string message = formatter(state, exception);
28+
sb.Append(message);
29+
30+
if (exception != null)
31+
{
32+
sb.Append(": ");
33+
sb.Append(exception);
34+
}
35+
36+
string line = sb.ToString();
37+
Console.WriteLine(line);
38+
}
39+
}
40+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#nullable enable
2+
3+
using Microsoft.Extensions.Logging;
4+
5+
namespace Renci.SshNet.IntegrationTests.Logging
6+
{
7+
internal class TestConsoleLoggerProvider : ILoggerProvider
8+
{
9+
public ILogger CreateLogger(string categoryName)
10+
{
11+
return new TestConsoleLogger(categoryName);
12+
}
13+
14+
public void Dispose()
15+
{
16+
}
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#nullable enable
2+
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.DependencyInjection.Extensions;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Renci.SshNet.IntegrationTests.Logging
8+
{
9+
internal static class TestConsoleLoggerProviderExtensions
10+
{
11+
internal static void AddTestConsoleLogger(this ILoggingBuilder builder)
12+
{
13+
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, TestConsoleLoggerProvider>());
14+
}
15+
}
16+
}

test/Renci.SshNet.IntegrationTests/TestsFixtures/InfrastructureFixture.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
using Microsoft.Extensions.Logging;
88

9+
using Renci.SshNet.IntegrationTests.Logging;
10+
911
namespace Renci.SshNet.IntegrationTests.TestsFixtures
1012
{
1113
public sealed class InfrastructureFixture : IDisposable
@@ -16,7 +18,7 @@ private InfrastructureFixture()
1618
{
1719
builder.SetMinimumLevel(LogLevel.Debug);
1820
builder.AddFilter("testcontainers", LogLevel.Information);
19-
builder.AddConsole();
21+
builder.AddTestConsoleLogger();
2022
});
2123

2224
SshNetLoggingConfiguration.InitializeLogging(_loggerFactory);

0 commit comments

Comments
 (0)