Skip to content

Commit 6d12f5d

Browse files
committed
hook up testcontainers logging
1 parent 3e46405 commit 6d12f5d

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

docfx/logging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Logging
22
=================
33

4-
SSH.NET uses the `Microsoft.Extensions.Logging` API to log diagnostic messages. In order to access the log messages of SSH.NET in your own application for diagnosis, register your own `ILoggerFactory` before using the SSH.NET APIs, for example:
4+
SSH.NET uses the [Microsoft.Extensions.Logging](https://learn.microsoft.com/dotnet/core/extensions/logging) API to log diagnostic messages. In order to access the log messages of SSH.NET in your own application for diagnosis, register your own `ILoggerFactory` before using the SSH.NET APIs, for example:
55

66
```cs
77
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22
using DotNet.Testcontainers.Containers;
33
using DotNet.Testcontainers.Images;
44

5+
using Microsoft.Extensions.Logging;
6+
57
namespace Renci.SshNet.IntegrationTests.TestsFixtures
68
{
79
public sealed class InfrastructureFixture : IDisposable
810
{
911
private InfrastructureFixture()
1012
{
13+
_loggerFactory = LoggerFactory.Create(builder =>
14+
{
15+
builder.SetMinimumLevel(LogLevel.Debug);
16+
builder.AddFilter("testcontainers", LogLevel.Information);
17+
builder.AddConsole();
18+
});
19+
20+
SshNetLoggingConfiguration.InitializeLogging(_loggerFactory);
1121
}
1222

13-
private static readonly Lazy<InfrastructureFixture> InstanceLazy = new Lazy<InfrastructureFixture>(() => new InfrastructureFixture());
23+
public static InfrastructureFixture Instance { get; } = new InfrastructureFixture();
1424

15-
public static InfrastructureFixture Instance
16-
{
17-
get
18-
{
19-
return InstanceLazy.Value;
20-
}
21-
}
25+
private readonly ILoggerFactory _loggerFactory;
2226

2327
private IContainer _sshServer;
2428

@@ -34,11 +38,14 @@ public static InfrastructureFixture Instance
3438

3539
public async Task InitializeAsync()
3640
{
41+
var containerLogger = _loggerFactory.CreateLogger("testcontainers");
42+
3743
_sshServerImage = new ImageFromDockerfileBuilder()
3844
.WithName("renci-ssh-tests-server-image")
3945
.WithDockerfileDirectory(CommonDirectoryPath.GetSolutionDirectory(), Path.Combine("test", "Renci.SshNet.IntegrationTests"))
4046
.WithDockerfile("Dockerfile.TestServer")
4147
.WithDeleteIfExists(true)
48+
.WithLogger(containerLogger)
4249
.Build();
4350

4451
await _sshServerImage.CreateAsync();
@@ -47,6 +54,7 @@ public async Task InitializeAsync()
4754
.WithHostname("renci-ssh-tests-server")
4855
.WithImage(_sshServerImage)
4956
.WithPortBinding(22, true)
57+
.WithLogger(containerLogger)
5058
.Build();
5159

5260
await _sshServer.StartAsync();

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

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Renci.SshNet.IntegrationTests.TestsFixtures
88
public abstract class IntegrationTestBase
99
{
1010
private readonly InfrastructureFixture _infrastructureFixture;
11+
private readonly ILogger _logger;
1112

1213
/// <summary>
1314
/// The SSH Server host name.
@@ -56,24 +57,10 @@ public SshUser User
5657
protected IntegrationTestBase()
5758
{
5859
_infrastructureFixture = InfrastructureFixture.Instance;
59-
ShowInfrastructureInformation();
60-
}
61-
62-
static IntegrationTestBase()
63-
{
64-
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
65-
{
66-
builder.SetMinimumLevel(LogLevel.Information);
67-
builder.AddConsole();
68-
});
69-
70-
SshNetLoggingConfiguration.InitializeLogging(loggerFactory);
71-
}
72-
73-
private void ShowInfrastructureInformation()
74-
{
75-
Console.WriteLine($"SSH Server host name: {_infrastructureFixture.SshServerHostName}");
76-
Console.WriteLine($"SSH Server port: {_infrastructureFixture.SshServerPort}");
60+
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
61+
_logger.LogDebug("SSH Server: {Host}:{Port}",
62+
_infrastructureFixture.SshServerHostName,
63+
_infrastructureFixture.SshServerPort);
7764
}
7865

7966
/// <summary>

0 commit comments

Comments
 (0)