Skip to content

Commit d7e4e15

Browse files
committed
Refactor logging to allow a loggerfactory per session specified in the ConnectionInfo.
This commit introduces an `ILoggerFactory` to various classes, replacing the static logger factory with an instance-based approach for more flexible and session-specific logging. These changes improve the logging framework's flexibility and maintainability and allow unit testing of logging.
1 parent fc988bd commit d7e4e15

32 files changed

+139
-46
lines changed

src/Renci.SshNet/BaseClient.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ public abstract class BaseClient : IBaseClient
2020
/// Holds value indicating whether the connection info is owned by this client.
2121
/// </summary>
2222
private readonly bool _ownsConnectionInfo;
23-
24-
private readonly ILogger _logger;
2523
private readonly IServiceFactory _serviceFactory;
2624
private readonly object _keepAliveLock = new object();
2725
private TimeSpan _keepAliveInterval;
@@ -37,6 +35,14 @@ public abstract class BaseClient : IBaseClient
3735
/// </value>
3836
internal ISession? Session { get; private set; }
3937

38+
internal ILogger? Logger
39+
{
40+
get
41+
{
42+
return Session?.SessionLoggerFactory?.CreateLogger(GetType());
43+
}
44+
}
45+
4046
/// <summary>
4147
/// Gets the factory for creating new services.
4248
/// </summary>
@@ -192,7 +198,6 @@ private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionI
192198
_connectionInfo = connectionInfo;
193199
_ownsConnectionInfo = ownsConnectionInfo;
194200
_serviceFactory = serviceFactory;
195-
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
196201
_keepAliveInterval = Timeout.InfiniteTimeSpan;
197202
}
198203

@@ -346,7 +351,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
346351
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
347352
public void Disconnect()
348353
{
349-
_logger.LogInformation("Disconnecting client.");
354+
Logger?.LogInformation("Disconnecting client.");
350355

351356
CheckDisposed();
352357

@@ -445,7 +450,7 @@ protected virtual void Dispose(bool disposing)
445450

446451
if (disposing)
447452
{
448-
_logger.LogDebug("Disposing client.");
453+
Logger?.LogDebug("Disposing client.");
449454

450455
Disconnect();
451456

@@ -508,7 +513,7 @@ private void SendKeepAliveMessage()
508513
}
509514
catch (Exception ex)
510515
{
511-
_logger.LogError(ex, "Error sending keepalive message");
516+
Logger?.LogError(ex, "Error sending keepalive message");
512517
}
513518
finally
514519
{

src/Renci.SshNet/Channels/Channel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected Channel(ISession session, uint localChannelNumber, uint localWindowSiz
8484
LocalChannelNumber = localChannelNumber;
8585
LocalPacketSize = localPacketSize;
8686
LocalWindowSize = localWindowSize;
87-
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
87+
_logger = session.SessionLoggerFactory.CreateLogger(GetType());
8888

8989
session.ChannelWindowAdjustReceived += OnChannelWindowAdjust;
9090
session.ChannelDataReceived += OnChannelData;

src/Renci.SshNet/Channels/ChannelDirectTcpip.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal sealed class ChannelDirectTcpip : ClientChannel, IChannelDirectTcpip
3333
public ChannelDirectTcpip(ISession session, uint localChannelNumber, uint localWindowSize, uint localPacketSize)
3434
: base(session, localChannelNumber, localWindowSize, localPacketSize)
3535
{
36-
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ChannelDirectTcpip>();
36+
_logger = session.SessionLoggerFactory.CreateLogger<ChannelDirectTcpip>();
3737
}
3838

3939
/// <summary>

src/Renci.SshNet/Channels/ChannelForwardedTcpip.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ internal ChannelForwardedTcpip(ISession session,
4848
remoteWindowSize,
4949
remotePacketSize)
5050
{
51-
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ChannelForwardedTcpip>();
51+
_logger = session.SessionLoggerFactory.CreateLogger<ChannelForwardedTcpip>();
5252
}
5353

5454
/// <summary>

src/Renci.SshNet/Connection/ConnectorBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ namespace Renci.SshNet.Connection
1515
internal abstract class ConnectorBase : IConnector
1616
{
1717
private readonly ILogger _logger;
18+
private readonly ILoggerFactory _loggerFactory;
1819

19-
protected ConnectorBase(ISocketFactory socketFactory)
20+
protected ConnectorBase(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
2021
{
2122
ThrowHelper.ThrowIfNull(socketFactory);
2223

2324
SocketFactory = socketFactory;
24-
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
25+
_loggerFactory = loggerFactory;
26+
_logger = _loggerFactory.CreateLogger(GetType());
2527
}
2628

2729
internal ISocketFactory SocketFactory { get; private set; }

src/Renci.SshNet/Connection/DirectConnector.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
using System.Net.Sockets;
33
using System.Threading;
44

5+
using Microsoft.Extensions.Logging;
6+
57
namespace Renci.SshNet.Connection
68
{
79
internal sealed class DirectConnector : ConnectorBase
810
{
9-
public DirectConnector(ISocketFactory socketFactory)
10-
: base(socketFactory)
11+
public DirectConnector(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
12+
: base(socketFactory, loggerFactory)
1113
{
1214
}
1315

src/Renci.SshNet/Connection/HttpConnector.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Net.Sockets;
66
using System.Text.RegularExpressions;
77

8+
using Microsoft.Extensions.Logging;
9+
810
using Renci.SshNet.Abstractions;
911
using Renci.SshNet.Common;
1012

@@ -48,8 +50,8 @@ internal sealed partial class HttpConnector : ProxyConnector
4850
private static readonly Regex HttpHeaderRegex = new Regex(HttpHeaderPattern, RegexOptions.Compiled);
4951
#endif
5052

51-
public HttpConnector(ISocketFactory socketFactory)
52-
: base(socketFactory)
53+
public HttpConnector(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
54+
: base(socketFactory, loggerFactory)
5355
{
5456
}
5557

src/Renci.SshNet/Connection/ProxyConnector.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Threading;
55
using System.Threading.Tasks;
66

7+
using Microsoft.Extensions.Logging;
8+
79
namespace Renci.SshNet.Connection
810
{
911
/// <summary>
@@ -12,8 +14,8 @@ namespace Renci.SshNet.Connection
1214
/// </summary>
1315
internal abstract class ProxyConnector : ConnectorBase
1416
{
15-
protected ProxyConnector(ISocketFactory socketFactory)
16-
: base(socketFactory)
17+
protected ProxyConnector(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
18+
: base(socketFactory, loggerFactory)
1719
{
1820
}
1921

src/Renci.SshNet/Connection/Socks4Connector.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Net.Sockets;
55
using System.Text;
66

7+
using Microsoft.Extensions.Logging;
8+
79
using Renci.SshNet.Abstractions;
810
using Renci.SshNet.Common;
911

@@ -17,8 +19,8 @@ namespace Renci.SshNet.Connection
1719
/// </remarks>
1820
internal sealed class Socks4Connector : ProxyConnector
1921
{
20-
public Socks4Connector(ISocketFactory socketFactory)
21-
: base(socketFactory)
22+
public Socks4Connector(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
23+
: base(socketFactory, loggerFactory)
2224
{
2325
}
2426

src/Renci.SshNet/Connection/Socks5Connector.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Net.Sockets;
66
using System.Text;
77

8+
using Microsoft.Extensions.Logging;
9+
810
using Renci.SshNet.Abstractions;
911
using Renci.SshNet.Common;
1012

@@ -18,8 +20,8 @@ namespace Renci.SshNet.Connection
1820
/// </remarks>
1921
internal sealed class Socks5Connector : ProxyConnector
2022
{
21-
public Socks5Connector(ISocketFactory socketFactory)
22-
: base(socketFactory)
23+
public Socks5Connector(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
24+
: base(socketFactory, loggerFactory)
2325
{
2426
}
2527

0 commit comments

Comments
 (0)