Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ project.lock.json

# Build outputs
build/target/
/.vs
31 changes: 22 additions & 9 deletions src/Renci.SshNet/Abstractions/DiagnosticAbstraction.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
using System.Diagnostics;
#if FEATURE_DIAGNOSTICS_TRACESOURCE
using System.Threading;
#endif // FEATURE_DIAGNOSTICS_TRACESOURCE

namespace Renci.SshNet.Abstractions
{
internal static class DiagnosticAbstraction
/// <summary>
/// Diagnostics for Renci library
/// </summary>
public static class DiagnosticAbstraction
{
#if FEATURE_DIAGNOSTICS_TRACESOURCE

private static readonly SourceSwitch SourceSwitch = new SourceSwitch("SshNetSwitch");

/// <summary>
/// Whether the specified event type is enabled for tracing or not
/// </summary>
/// <param name="traceEventType">The trace event type</param>
/// <returns>true if enabled for tracing, false otherwise</returns>
public static bool IsEnabled(TraceEventType traceEventType)
{
return SourceSwitch.ShouldTrace(traceEventType);
}

private static readonly TraceSource Loggging =
/// <summary>
/// The trace source for Renci
/// </summary>
public static readonly TraceSource Logging =
#if DEBUG
new TraceSource("SshNet.Logging", SourceLevels.All);
new TraceSource(name: "SshNet.Logging", defaultLevel: SourceLevels.All);
#else
new TraceSource("SshNet.Logging");
#endif // DEBUG
#endif // FEATURE_DIAGNOSTICS_TRACESOURCE

[Conditional("DEBUG")]
public static void Log(string text)
/// <summary>
/// Log the provided text
/// </summary>
/// <param name="text">The text string to log</param>
/// <param name="eventType">The trace event type</param>
/// <param name="id">A numeric identifier for the event.</param>
public static void Log(string text, TraceEventType eventType = TraceEventType.Verbose, TraceEventId id = TraceEventId._default)
{
#if FEATURE_DIAGNOSTICS_TRACESOURCE
Loggging.TraceEvent(TraceEventType.Verbose, Thread.CurrentThread.ManagedThreadId, text);
Logging.TraceEvent(eventType, (int)id, text);
#endif // FEATURE_DIAGNOSTICS_TRACESOURCE
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Renci.SshNet/BaseClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Net.Sockets;
using System.Threading;
using Renci.SshNet.Abstractions;
Expand Down Expand Up @@ -245,7 +246,7 @@ public void Connect()
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
public void Disconnect()
{
DiagnosticAbstraction.Log("Disconnecting client.");
DiagnosticAbstraction.Log("Disconnecting client.", TraceEventType.Verbose, TraceEventId.DisconnectingClient);

CheckDisposed();

Expand Down Expand Up @@ -336,7 +337,7 @@ private void Session_HostKeyReceived(object sender, HostKeyEventArgs e)
/// </summary>
public void Dispose()
{
DiagnosticAbstraction.Log("Disposing client.");
DiagnosticAbstraction.Log("Disposing client.", TraceEventType.Verbose, TraceEventId.DisposingClient);

Dispose(true);
GC.SuppressFinalize(this);
Expand Down
7 changes: 4 additions & 3 deletions src/Renci.SshNet/Channels/Channel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.Net.Sockets;
using System.Threading;
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
using Renci.SshNet.Messages;
using Renci.SshNet.Messages.Connection;
using System.Globalization;
using Renci.SshNet.Abstractions;

namespace Renci.SshNet.Channels
{
Expand Down Expand Up @@ -551,7 +552,7 @@ protected virtual void Close()
var closeWaitResult = _session.TryWait(_channelClosedWaitHandle, ConnectionInfo.ChannelCloseTimeout);
if (closeWaitResult != WaitResult.Success)
{
DiagnosticAbstraction.Log(string.Format("Wait for channel close not successful: {0:G}.", closeWaitResult));
DiagnosticAbstraction.Log($"Wait for channel close not successful: {closeWaitResult:G}.", TraceEventType.Warning, TraceEventId.UnsuccessfulChannelCloseWait);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet/Channels/ChannelDirectTcpip.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading;
Expand Down Expand Up @@ -138,8 +139,7 @@ private void ShutdownSocket(SocketShutdown how)
}
catch (SocketException ex)
{
// TODO: log as warning
DiagnosticAbstraction.Log("Failure shutting down socket: " + ex);
DiagnosticAbstraction.Log($"Failure shutting down socket: {ex}", TraceEventType.Warning, TraceEventId.SocketShutdownFailure);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet/Channels/ChannelForwardedTcpip.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using Renci.SshNet.Abstractions;
Expand Down Expand Up @@ -134,8 +135,7 @@ private void ShutdownSocket(SocketShutdown how)
}
catch (SocketException ex)
{
// TODO: log as warning
DiagnosticAbstraction.Log("Failure shutting down socket: " + ex);
DiagnosticAbstraction.Log($"Failure shutting down socket: {ex}", TraceEventType.Warning, TraceEventId.SocketShutdownFailure);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet/ForwardedPortDynamic.NET.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Net;
Expand Down Expand Up @@ -266,8 +267,7 @@ partial void InternalStop(TimeSpan timeout)
_pendingChannelCountdown.Signal();
if (!_pendingChannelCountdown.Wait(timeout))
{
// TODO: log as warning
DiagnosticAbstraction.Log("Timeout waiting for pending channels in dynamic forwarded port to close.");
DiagnosticAbstraction.Log("Timeout waiting for pending channels in dynamic forwarded port to close.", TraceEventType.Warning, TraceEventId.PendingChannelsCloseTimeout);
}

}
Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet/ForwardedPortLocal.NET.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Net.Sockets;
using System.Net;
using System.Threading;
Expand Down Expand Up @@ -216,8 +217,7 @@ partial void InternalStop(TimeSpan timeout)
_pendingChannelCountdown.Signal();
if (!_pendingChannelCountdown.Wait(timeout))
{
// TODO: log as warning
DiagnosticAbstraction.Log("Timeout waiting for pending channels in local forwarded port to close.");
DiagnosticAbstraction.Log("Timeout waiting for pending channels in local forwarded port to close.", TraceEventType.Warning, TraceEventId.PendingChannelsCloseTimeout);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Renci.SshNet/ForwardedPortRemote.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Threading;
using Renci.SshNet.Messages.Connection;
using Renci.SshNet.Common;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Threading;
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Connection;

namespace Renci.SshNet
{
Expand Down Expand Up @@ -204,8 +205,7 @@ protected override void StopPort(TimeSpan timeout)

if (!_pendingChannelCountdown.Wait(timeout))
{
// TODO: log as warning
DiagnosticAbstraction.Log("Timeout waiting for pending channels in remote forwarded port to close.");
DiagnosticAbstraction.Log("Timeout waiting for pending channels in remote forwarded port to close.", TraceEventType.Warning, TraceEventId.PendingChannelsCloseTimeout);
}

_status = ForwardedPortStatus.Stopped;
Expand Down
3 changes: 2 additions & 1 deletion src/Renci.SshNet/Messages/Transport/IgnoreMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Globalization;
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
Expand Down Expand Up @@ -68,7 +69,7 @@ protected override void LoadData()

if (dataLength > (DataStream.Length - DataStream.Position))
{
DiagnosticAbstraction.Log("SSH_MSG_IGNORE: Length exceeds data bytes, data ignored.");
DiagnosticAbstraction.Log("SSH_MSG_IGNORE: Length exceeds data bytes, data ignored.", TraceEventType.Warning, TraceEventId.ExcessData);
Data = Array<byte>.Empty;
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Renci.SshNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AssemblyName>Renci.SshNet</AssemblyName>
<AssemblyOriginatorKeyFile>../Renci.SshNet.snk</AssemblyOriginatorKeyFile>
<LangVersion>5</LangVersion>
<LangVersion>6</LangVersion>
<SignAssembly>true</SignAssembly>
<TargetFrameworks>net35;net40;netstandard1.3;netstandard2.0</TargetFrameworks>
</PropertyGroup>
Expand Down
7 changes: 2 additions & 5 deletions src/Renci.SshNet/Security/KeyExchange.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using Renci.SshNet.Abstractions;
Expand Down Expand Up @@ -187,11 +188,7 @@ public Cipher CreateServerCipher()

serverKey = GenerateSessionKey(SharedKey, ExchangeHash, serverKey, _serverCipherInfo.KeySize / 8);

DiagnosticAbstraction.Log(string.Format("[{0}] Creating server cipher (Name:{1},Key:{2},IV:{3})",
Session.ToHex(Session.SessionId),
Session.ConnectionInfo.CurrentServerEncryption,
Session.ToHex(serverKey),
Session.ToHex(serverVector)));
DiagnosticAbstraction.Log($"[{Session.ToHex(Session.SessionId)}] Creating server cipher (Name:{Session.ConnectionInfo.CurrentServerEncryption},Key:{Session.ToHex(serverKey)},IV:{Session.ToHex(serverVector)})", TraceEventType.Verbose, TraceEventId.ServerCipherCreation);

// Create server cipher
return _serverCipherInfo.Cipher(serverKey, serverVector);
Expand Down
5 changes: 3 additions & 2 deletions src/Renci.SshNet/ServiceFactory.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Transport;
using Renci.SshNet.Security;
using Renci.SshNet.Sftp;
using Renci.SshNet.Abstractions;

namespace Renci.SshNet
{
Expand Down Expand Up @@ -134,7 +135,7 @@ public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSe
fileSize = null;
maxPendingReads = defaultMaxPendingReads;

DiagnosticAbstraction.Log(string.Format("Failed to obtain size of file. Allowing maximum {0} pending reads: {1}", maxPendingReads, ex));
DiagnosticAbstraction.Log($"Failed to obtain size of file. Allowing maximum {maxPendingReads} pending reads: { ex}", TraceEventType.Warning, TraceEventId.FileSizeFetchFailure);
}

return sftpSession.CreateFileReader(handle, sftpSession, chunkSize, maxPendingReads, fileSize);
Expand Down
Loading