Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Renci.SshNet/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public abstract class BaseClient : IBaseClient
/// Holds value indicating whether the connection info is owned by this client.
/// </summary>
private readonly bool _ownsConnectionInfo;

private readonly ILogger _logger;
private readonly IServiceFactory _serviceFactory;
private readonly object _keepAliveLock = new object();
private TimeSpan _keepAliveInterval;
Expand All @@ -37,6 +35,14 @@ public abstract class BaseClient : IBaseClient
/// </value>
internal ISession? Session { get; private set; }

internal ILogger? Logger
{
get
{
return Session?.SessionLoggerFactory?.CreateLogger(GetType());
}
}

/// <summary>
/// Gets the factory for creating new services.
/// </summary>
Expand Down Expand Up @@ -192,7 +198,6 @@ private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionI
_connectionInfo = connectionInfo;
_ownsConnectionInfo = ownsConnectionInfo;
_serviceFactory = serviceFactory;
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
_keepAliveInterval = Timeout.InfiniteTimeSpan;
}

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

CheckDisposed();

Expand Down Expand Up @@ -445,7 +450,7 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
_logger.LogDebug("Disposing client.");
Logger?.LogDebug("Disposing client.");

Disconnect();

Expand Down Expand Up @@ -508,7 +513,7 @@ private void SendKeepAliveMessage()
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending keepalive message");
Logger?.LogError(ex, "Error sending keepalive message");
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Channels/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected Channel(ISession session, uint localChannelNumber, uint localWindowSiz
LocalChannelNumber = localChannelNumber;
LocalPacketSize = localPacketSize;
LocalWindowSize = localWindowSize;
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
_logger = session.SessionLoggerFactory.CreateLogger(GetType());

session.ChannelWindowAdjustReceived += OnChannelWindowAdjust;
session.ChannelDataReceived += OnChannelData;
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Channels/ChannelDirectTcpip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal sealed class ChannelDirectTcpip : ClientChannel, IChannelDirectTcpip
public ChannelDirectTcpip(ISession session, uint localChannelNumber, uint localWindowSize, uint localPacketSize)
: base(session, localChannelNumber, localWindowSize, localPacketSize)
{
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ChannelDirectTcpip>();
_logger = session.SessionLoggerFactory.CreateLogger<ChannelDirectTcpip>();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Channels/ChannelForwardedTcpip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal ChannelForwardedTcpip(ISession session,
remoteWindowSize,
remotePacketSize)
{
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ChannelForwardedTcpip>();
_logger = session.SessionLoggerFactory.CreateLogger<ChannelForwardedTcpip>();
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions src/Renci.SshNet/Connection/ConnectorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ namespace Renci.SshNet.Connection
internal abstract class ConnectorBase : IConnector
{
private readonly ILogger _logger;
private readonly ILoggerFactory _loggerFactory;

protected ConnectorBase(ISocketFactory socketFactory)
protected ConnectorBase(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
{
ThrowHelper.ThrowIfNull(socketFactory);

SocketFactory = socketFactory;
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
_loggerFactory = loggerFactory;
_logger = _loggerFactory.CreateLogger(GetType());
}

internal ISocketFactory SocketFactory { get; private set; }
Expand Down
6 changes: 4 additions & 2 deletions src/Renci.SshNet/Connection/DirectConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
using System.Net.Sockets;
using System.Threading;

using Microsoft.Extensions.Logging;

namespace Renci.SshNet.Connection
{
internal sealed class DirectConnector : ConnectorBase
{
public DirectConnector(ISocketFactory socketFactory)
: base(socketFactory)
public DirectConnector(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
: base(socketFactory, loggerFactory)
{
}

Expand Down
6 changes: 4 additions & 2 deletions src/Renci.SshNet/Connection/HttpConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Net.Sockets;
using System.Text.RegularExpressions;

using Microsoft.Extensions.Logging;

using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;

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

public HttpConnector(ISocketFactory socketFactory)
: base(socketFactory)
public HttpConnector(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
: base(socketFactory, loggerFactory)
{
}

Expand Down
6 changes: 4 additions & 2 deletions src/Renci.SshNet/Connection/ProxyConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Extensions.Logging;

namespace Renci.SshNet.Connection
{
/// <summary>
Expand All @@ -12,8 +14,8 @@ namespace Renci.SshNet.Connection
/// </summary>
internal abstract class ProxyConnector : ConnectorBase
{
protected ProxyConnector(ISocketFactory socketFactory)
: base(socketFactory)
protected ProxyConnector(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
: base(socketFactory, loggerFactory)
{
}

Expand Down
6 changes: 4 additions & 2 deletions src/Renci.SshNet/Connection/Socks4Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Net.Sockets;
using System.Text;

using Microsoft.Extensions.Logging;

using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;

Expand All @@ -17,8 +19,8 @@ namespace Renci.SshNet.Connection
/// </remarks>
internal sealed class Socks4Connector : ProxyConnector
{
public Socks4Connector(ISocketFactory socketFactory)
: base(socketFactory)
public Socks4Connector(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
: base(socketFactory, loggerFactory)
{
}

Expand Down
6 changes: 4 additions & 2 deletions src/Renci.SshNet/Connection/Socks5Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Net.Sockets;
using System.Text;

using Microsoft.Extensions.Logging;

using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;

Expand All @@ -18,8 +20,8 @@ namespace Renci.SshNet.Connection
/// </remarks>
internal sealed class Socks5Connector : ProxyConnector
{
public Socks5Connector(ISocketFactory socketFactory)
: base(socketFactory)
public Socks5Connector(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
: base(socketFactory, loggerFactory)
{
}

Expand Down
10 changes: 10 additions & 0 deletions src/Renci.SshNet/ConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Security.Cryptography;
using System.Text;

using Microsoft.Extensions.Logging;

using Renci.SshNet.Common;
using Renci.SshNet.Compression;
using Renci.SshNet.Messages.Authentication;
Expand Down Expand Up @@ -495,5 +497,13 @@ IList<IAuthenticationMethod> IConnectionInfoInternal.AuthenticationMethods
get { return AuthenticationMethods.Cast<IAuthenticationMethod>().ToList(); }
#pragma warning restore S2365 // Properties should not make collection or array copies
}

/// <summary>
/// Gets or sets logger factory for this connection.
/// </summary>
/// <value>
/// The logger factory for this connection. If <see langword="null"/> then <see cref="SshNetLoggingConfiguration.LoggerFactory"/> should be used.
/// </value>
public ILoggerFactory LoggerFactory { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Renci.SshNet/ForwardedPortDynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public ForwardedPortDynamic(string host, uint port)
BoundHost = host;
BoundPort = port;
_status = ForwardedPortStatus.Stopped;
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ForwardedPortDynamic>();
_logger = Session.SessionLoggerFactory.CreateLogger<ForwardedPortDynamic>();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/ForwardedPortLocal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public ForwardedPortLocal(string boundHost, uint boundPort, string host, uint po
Host = host;
Port = port;
_status = ForwardedPortStatus.Stopped;
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ForwardedPortLocal>();
_logger = Session.SessionLoggerFactory.CreateLogger<ForwardedPortLocal>();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/ForwardedPortRemote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public ForwardedPortRemote(IPAddress boundHostAddress, uint boundPort, IPAddress
HostAddress = hostAddress;
Port = port;
_status = ForwardedPortStatus.Stopped;
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ForwardedPortRemote>();
_logger = Session.SessionLoggerFactory.CreateLogger<ForwardedPortRemote>();
}

/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Renci.SshNet/IConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Text;

using Microsoft.Extensions.Logging;

using Renci.SshNet.Common;
using Renci.SshNet.Messages.Connection;

Expand All @@ -12,6 +14,14 @@ namespace Renci.SshNet
/// </summary>
internal interface IConnectionInfo
{
/// <summary>
/// Gets the logger factory for this connection.
/// </summary>
/// <value>
/// The logger factory for this connection. If <see langword="null"/> then <see cref="SshNetLoggingConfiguration.LoggerFactory"/> should be used.
/// </value>
public ILoggerFactory LoggerFactory { get; }

/// <summary>
/// Gets the timeout to used when waiting for a server to acknowledge closing a channel.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Renci.SshNet/ISession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Extensions.Logging;

using Renci.SshNet.Channels;
using Renci.SshNet.Common;
using Renci.SshNet.Messages;
Expand All @@ -22,6 +24,14 @@ internal interface ISession : IDisposable
/// <value>The connection info.</value>
IConnectionInfo ConnectionInfo { get; }

/// <summary>
/// Gets the logger factory for this session.
/// </summary>
/// <value>
/// The logger factory for this session.
/// </value>
public ILoggerFactory SessionLoggerFactory { get; }

/// <summary>
/// Gets a value indicating whether the session is connected.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Renci.SshNet/ISubsystemSession.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Threading;

using Microsoft.Extensions.Logging;

using Renci.SshNet.Common;

namespace Renci.SshNet
Expand All @@ -10,6 +12,14 @@ namespace Renci.SshNet
/// </summary>
internal interface ISubsystemSession : IDisposable
{
/// <summary>
/// Gets the logger factory for this subsystem session.
/// </summary>
/// <value>
/// The logger factory for this connection.
/// </value>
public ILoggerFactory SessionLoggerFactory { get; }

/// <summary>
/// Gets or sets the number of milliseconds to wait for an operation to complete.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Security/KeyExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public byte[] ExchangeHash
/// </summary>
protected KeyExchange()
{
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
_logger = Session.SessionLoggerFactory.CreateLogger(GetType());
}

/// <inheritdoc/>
Expand Down
15 changes: 7 additions & 8 deletions src/Renci.SshNet/ServiceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ internal sealed partial class ServiceFactory : IServiceFactory
/// </summary>
private const int PartialSuccessLimit = 5;

private readonly ILogger _logger;

internal ServiceFactory()
{
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ServiceFactory>();
}

/// <summary>
Expand Down Expand Up @@ -160,7 +157,7 @@ public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSe
fileSize = null;
maxPendingReads = defaultMaxPendingReads;

_logger.LogInformation(ex, "Failed to obtain size of file. Allowing maximum {MaxPendingReads} pending reads", maxPendingReads);
sftpSession.SessionLoggerFactory.CreateLogger<ServiceFactory>().LogInformation(ex, "Failed to obtain size of file. Allowing maximum {MaxPendingReads} pending reads", maxPendingReads);
}

return sftpSession.CreateFileReader(handle, sftpSession, chunkSize, maxPendingReads, fileSize);
Expand Down Expand Up @@ -221,16 +218,18 @@ public IConnector CreateConnector(IConnectionInfo connectionInfo, ISocketFactory
ThrowHelper.ThrowIfNull(connectionInfo);
ThrowHelper.ThrowIfNull(socketFactory);

var loggerFactory = connectionInfo.LoggerFactory ?? SshNetLoggingConfiguration.LoggerFactory;

switch (connectionInfo.ProxyType)
{
case ProxyTypes.None:
return new DirectConnector(socketFactory);
return new DirectConnector(socketFactory, loggerFactory);
case ProxyTypes.Socks4:
return new Socks4Connector(socketFactory);
return new Socks4Connector(socketFactory, loggerFactory);
case ProxyTypes.Socks5:
return new Socks5Connector(socketFactory);
return new Socks5Connector(socketFactory, loggerFactory);
case ProxyTypes.Http:
return new HttpConnector(socketFactory);
return new HttpConnector(socketFactory, loggerFactory);
default:
throw new NotSupportedException(string.Format("ProxyTypes '{0}' is not supported.", connectionInfo.ProxyType));
}
Expand Down
Loading
Loading