Skip to content

Commit 553e47f

Browse files
committed
Replace DiagnosticAbstrations with Microsoft.Extensions.Logging.Abstractions
1 parent 1a8839e commit 553e47f

20 files changed

+151
-139
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,9 @@ dotnet_code_quality.CA1828.api_surface = all
701701
# Similar to MA0053, but does not support public types and types that define (new) virtual members.
702702
dotnet_diagnostic.CA1852.severity = none
703703

704+
# CA1848: don't enforce LoggerMessage pattern
705+
dotnet_diagnostic.CA1848.severity = suggestion
706+
704707
# CA1859: Change return type for improved performance
705708
#
706709
# By default, this diagnostic is only reported for private members.

Directory.Packages.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<PackageVersion Include="Meziantou.Analyzer" Version="2.0.163" />
1414
<!-- Must be kept at version 1.0.0 or higher, see https://github.com/sshnet/SSH.NET/pull/1288 for details. -->
1515
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="1.0.0" />
16+
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
17+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
18+
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
1619
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
1720
<PackageVersion Include="MSTest.TestAdapter" Version="3.6.0" />
1821
<PackageVersion Include="MSTest.TestFramework" Version="3.6.0" />

src/Renci.SshNet/Abstractions/DiagnosticAbstraction.cs

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/Renci.SshNet/BaseClient.cs

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

7-
using Renci.SshNet.Abstractions;
7+
using Microsoft.Extensions.Logging;
8+
89
using Renci.SshNet.Common;
910
using Renci.SshNet.Messages.Transport;
1011

@@ -20,6 +21,7 @@ public abstract class BaseClient : IBaseClient
2021
/// </summary>
2122
private readonly bool _ownsConnectionInfo;
2223

24+
private readonly ILogger _logger;
2325
private readonly IServiceFactory _serviceFactory;
2426
private readonly object _keepAliveLock = new object();
2527
private TimeSpan _keepAliveInterval;
@@ -190,6 +192,7 @@ private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionI
190192
_connectionInfo = connectionInfo;
191193
_ownsConnectionInfo = ownsConnectionInfo;
192194
_serviceFactory = serviceFactory;
195+
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
193196
_keepAliveInterval = Timeout.InfiniteTimeSpan;
194197
}
195198

@@ -343,7 +346,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
343346
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
344347
public void Disconnect()
345348
{
346-
DiagnosticAbstraction.Log("Disconnecting client.");
349+
_logger.LogInformation("Disconnecting client.");
347350

348351
CheckDisposed();
349352

@@ -442,7 +445,7 @@ protected virtual void Dispose(bool disposing)
442445

443446
if (disposing)
444447
{
445-
DiagnosticAbstraction.Log("Disposing client.");
448+
_logger.LogInformation("Disposing client.");
446449

447450
Disconnect();
448451

src/Renci.SshNet/Channels/Channel.cs

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

5-
using Renci.SshNet.Abstractions;
5+
using Microsoft.Extensions.Logging;
6+
67
using Renci.SshNet.Common;
78
using Renci.SshNet.Messages;
89
using Renci.SshNet.Messages.Connection;
@@ -18,6 +19,7 @@ internal abstract class Channel : IChannel
1819
private readonly object _messagingLock = new object();
1920
private readonly uint _initialWindowSize;
2021
private readonly ISession _session;
22+
private readonly ILogger _logger;
2123
private EventWaitHandle _channelClosedWaitHandle = new ManualResetEvent(initialState: false);
2224
private EventWaitHandle _channelServerWindowAdjustWaitHandle = new ManualResetEvent(initialState: false);
2325
private uint? _remoteWindowSize;
@@ -81,6 +83,7 @@ protected Channel(ISession session, uint localChannelNumber, uint localWindowSiz
8183
LocalChannelNumber = localChannelNumber;
8284
LocalPacketSize = localPacketSize;
8385
LocalWindowSize = localWindowSize;
86+
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
8487

8588
session.ChannelWindowAdjustReceived += OnChannelWindowAdjust;
8689
session.ChannelDataReceived += OnChannelData;
@@ -555,7 +558,7 @@ protected virtual void Close()
555558
var closeWaitResult = _session.TryWait(_channelClosedWaitHandle, ConnectionInfo.ChannelCloseTimeout);
556559
if (closeWaitResult != WaitResult.Success)
557560
{
558-
DiagnosticAbstraction.Log(string.Format("Wait for channel close not successful: {0:G}.", closeWaitResult));
561+
_logger.LogInformation("Wait for channel close not successful: {CloseWaitResult}", closeWaitResult);
559562
}
560563
}
561564
}

src/Renci.SshNet/Channels/ChannelDirectTcpip.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Net.Sockets;
44
using System.Threading;
55

6+
using Microsoft.Extensions.Logging;
7+
68
using Renci.SshNet.Abstractions;
79
using Renci.SshNet.Common;
810
using Renci.SshNet.Messages.Connection;
@@ -15,6 +17,7 @@ namespace Renci.SshNet.Channels
1517
internal sealed class ChannelDirectTcpip : ClientChannel, IChannelDirectTcpip
1618
{
1719
private readonly object _socketLock = new object();
20+
private readonly ILogger _logger;
1821

1922
private EventWaitHandle _channelOpen = new AutoResetEvent(initialState: false);
2023
private EventWaitHandle _channelData = new AutoResetEvent(initialState: false);
@@ -31,6 +34,7 @@ internal sealed class ChannelDirectTcpip : ClientChannel, IChannelDirectTcpip
3134
public ChannelDirectTcpip(ISession session, uint localChannelNumber, uint localWindowSize, uint localPacketSize)
3235
: base(session, localChannelNumber, localWindowSize, localPacketSize)
3336
{
37+
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ChannelDirectTcpip>();
3438
}
3539

3640
/// <summary>
@@ -157,8 +161,7 @@ private void ShutdownSocket(SocketShutdown how)
157161
}
158162
catch (SocketException ex)
159163
{
160-
// TODO: log as warning
161-
DiagnosticAbstraction.Log("Failure shutting down socket: " + ex);
164+
_logger.LogWarning(ex, "Failure shutting down socket");
162165
}
163166
}
164167
}

src/Renci.SshNet/Channels/ChannelForwardedTcpip.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Net;
33
using System.Net.Sockets;
44

5+
using Microsoft.Extensions.Logging;
6+
57
using Renci.SshNet.Abstractions;
68
using Renci.SshNet.Common;
79
using Renci.SshNet.Messages.Connection;
@@ -14,6 +16,7 @@ namespace Renci.SshNet.Channels
1416
internal sealed class ChannelForwardedTcpip : ServerChannel, IChannelForwardedTcpip
1517
{
1618
private readonly object _socketShutdownAndCloseLock = new object();
19+
private readonly ILogger _logger;
1720
private Socket _socket;
1821
private IForwardedPort _forwardedPort;
1922

@@ -42,6 +45,7 @@ internal ChannelForwardedTcpip(ISession session,
4245
remoteWindowSize,
4346
remotePacketSize)
4447
{
48+
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ChannelForwardedTcpip>();
4549
}
4650

4751
/// <summary>
@@ -139,8 +143,7 @@ private void ShutdownSocket(SocketShutdown how)
139143
}
140144
catch (SocketException ex)
141145
{
142-
// TODO: log as warning
143-
DiagnosticAbstraction.Log("Failure shutting down socket: " + ex);
146+
_logger.LogWarning(ex, "Failure shutting down socket");
144147
}
145148
}
146149
}

src/Renci.SshNet/Connection/ConnectorBase.cs

Lines changed: 7 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
using Renci.SshNet.Abstractions;
810
using Renci.SshNet.Common;
911
using Renci.SshNet.Messages.Transport;
@@ -12,11 +14,14 @@ namespace Renci.SshNet.Connection
1214
{
1315
internal abstract class ConnectorBase : IConnector
1416
{
17+
private readonly ILogger _logger;
18+
1519
protected ConnectorBase(ISocketFactory socketFactory)
1620
{
1721
ThrowHelper.ThrowIfNull(socketFactory);
1822

1923
SocketFactory = socketFactory;
24+
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
2025
}
2126

2227
internal ISocketFactory SocketFactory { get; private set; }
@@ -34,7 +39,7 @@ protected ConnectorBase(ISocketFactory socketFactory)
3439
/// <exception cref="SocketException">An error occurred trying to establish the connection.</exception>
3540
protected Socket SocketConnect(EndPoint endPoint, TimeSpan timeout)
3641
{
37-
DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}'.", endPoint));
42+
_logger.LogInformation("Initiating connection to '{EndPoint}'.", endPoint);
3843

3944
var socket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
4045

@@ -65,7 +70,7 @@ protected async Task<Socket> SocketConnectAsync(EndPoint endPoint, CancellationT
6570
{
6671
cancellationToken.ThrowIfCancellationRequested();
6772

68-
DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}'.", endPoint));
73+
_logger.LogInformation("Initiating connection to '{EndPoint}'.", endPoint);
6974

7075
var socket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
7176
try

src/Renci.SshNet/ForwardedPortDynamic.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using System.Text;
88
using System.Threading;
99

10+
using Microsoft.Extensions.Logging;
11+
1012
using Renci.SshNet.Abstractions;
1113
using Renci.SshNet.Channels;
1214
using Renci.SshNet.Common;
@@ -19,6 +21,7 @@ namespace Renci.SshNet
1921
/// </summary>
2022
public class ForwardedPortDynamic : ForwardedPort
2123
{
24+
private readonly ILogger _logger;
2225
private ForwardedPortStatus _status;
2326

2427
/// <summary>
@@ -72,6 +75,7 @@ public ForwardedPortDynamic(string host, uint port)
7275
BoundHost = host;
7376
BoundPort = port;
7477
_status = ForwardedPortStatus.Stopped;
78+
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ForwardedPortDynamic>();
7579
}
7680

7781
/// <summary>
@@ -409,8 +413,7 @@ private void InternalStop(TimeSpan timeout)
409413

410414
if (!_pendingChannelCountdown.Wait(timeout))
411415
{
412-
// TODO: log as warning
413-
DiagnosticAbstraction.Log("Timeout waiting for pending channels in dynamic forwarded port to close.");
416+
_logger.LogWarning("Timeout waiting for pending channels in dynamic forwarded port to close.");
414417
}
415418
}
416419

src/Renci.SshNet/ForwardedPortLocal.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
using System.Net.Sockets;
44
using System.Threading;
55

6-
using Renci.SshNet.Abstractions;
6+
using Microsoft.Extensions.Logging;
7+
78
using Renci.SshNet.Common;
89

910
namespace Renci.SshNet
@@ -13,6 +14,7 @@ namespace Renci.SshNet
1314
/// </summary>
1415
public partial class ForwardedPortLocal : ForwardedPort
1516
{
17+
private readonly ILogger _logger;
1618
private ForwardedPortStatus _status;
1719
private bool _isDisposed;
1820
private Socket _listener;
@@ -101,6 +103,7 @@ public ForwardedPortLocal(string boundHost, uint boundPort, string host, uint po
101103
Host = host;
102104
Port = port;
103105
_status = ForwardedPortStatus.Stopped;
106+
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ForwardedPortLocal>();
104107
}
105108

106109
/// <summary>
@@ -387,8 +390,7 @@ private void InternalStop(TimeSpan timeout)
387390

388391
if (!_pendingChannelCountdown.Wait(timeout))
389392
{
390-
// TODO: log as warning
391-
DiagnosticAbstraction.Log("Timeout waiting for pending channels in local forwarded port to close.");
393+
_logger.LogWarning("Timeout waiting for pending channels in local forwarded port to close.");
392394
}
393395
}
394396

0 commit comments

Comments
 (0)