Skip to content

Commit 27903c8

Browse files
committed
add test case for per-client logging
1 parent 4963ba9 commit 27903c8

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageVersion Include="coverlet.msbuild" Version="6.0.4" />
1111
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
1212
<PackageVersion Include="Meziantou.Analyzer" Version="2.0.205" />
13+
<PackageVersion Include="Microsoft.Extensions.Diagnostics.Testing" Version="8.4.0" />
1314
<!-- Should stay on LTS .NET releases. -->
1415
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.3" />
1516
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.7" />
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#if NET
2+
using Microsoft.Extensions.Logging;
3+
using Microsoft.Extensions.Logging.Testing;
4+
5+
namespace Renci.SshNet.IntegrationTests
6+
{
7+
[TestClass]
8+
public class LoggingTest : IntegrationTestBase
9+
{
10+
private readonly SftpClient _sftpClient;
11+
12+
public LoggingTest()
13+
{
14+
_sftpClient = new SftpClient(SshServerHostName, SshServerPort, User.UserName, User.Password);
15+
}
16+
17+
[TestMethod]
18+
public async Task SessionLoggerFactoryTest()
19+
{
20+
// remember the original logger factory and restore it later
21+
ILoggerFactory originalTestLoggerFactory = SshNetLoggingConfiguration.LoggerFactory;
22+
23+
try
24+
{
25+
FakeLogCollector globalLogCollector = new();
26+
FakeLogCollector sessionLogCollector = new();
27+
28+
ILoggerFactory globalLoggerFactory = CreateFakeLoggerFactory(globalLogCollector);
29+
SshNetLoggingConfiguration.InitializeLogging(globalLoggerFactory);
30+
31+
_sftpClient.ConnectionInfo.LoggerFactory = CreateFakeLoggerFactory(sessionLogCollector);
32+
33+
await _sftpClient.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
34+
35+
// all logs should have been written to the session logger only
36+
Assert.AreEqual(0, globalLogCollector.Count);
37+
Assert.IsTrue(sessionLogCollector.Count > 0);
38+
}
39+
finally
40+
{
41+
SshNetLoggingConfiguration.InitializeLogging(originalTestLoggerFactory);
42+
}
43+
}
44+
45+
46+
private ILoggerFactory CreateFakeLoggerFactory(FakeLogCollector fakeLogCollector)
47+
{
48+
FakeLoggerProvider fakeLogProvider = new(fakeLogCollector);
49+
50+
return LoggerFactory.Create(builder =>
51+
{
52+
builder.AddProvider(fakeLogProvider);
53+
builder.SetMinimumLevel(LogLevel.Trace);
54+
});
55+
}
56+
57+
[TestCleanup]
58+
public void Cleanup()
59+
{
60+
_sftpClient.Disconnect();
61+
_sftpClient.Dispose();
62+
}
63+
}
64+
}
65+
#endif

test/Renci.SshNet.IntegrationTests/Renci.SshNet.IntegrationTests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
</PackageReference>
3131
</ItemGroup>
3232

33+
<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
34+
<PackageReference Include="Microsoft.Extensions.Diagnostics.Testing" />
35+
</ItemGroup>
36+
3337
<ItemGroup>
3438
<ProjectReference Include="..\Renci.SshNet.TestTools.OpenSSH\Renci.SshNet.TestTools.OpenSSH.csproj" />
3539
<ProjectReference Include="..\..\src\Renci.SshNet\Renci.SshNet.csproj" />

0 commit comments

Comments
 (0)