Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracing levels and actual trace event Ids #738

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 20 additions & 6 deletions src/Renci.SshNet/Abstractions/DiagnosticAbstraction.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
using System.Diagnostics;
#if FEATURE_DIAGNOSTICS_TRACESOURCE
using System.Threading;
#endif // FEATURE_DIAGNOSTICS_TRACESOURCE

namespace Renci.SshNet.Abstractions
{
/// <summary>
/// Diagnostics for Renci library
/// </summary>
internal 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);
#else
new TraceSource("SshNet.Logging");
#endif // DEBUG
#endif // FEATURE_DIAGNOSTICS_TRACESOURCE

/// <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>
[Conditional("DEBUG")]
daviburg marked this conversation as resolved.
Show resolved Hide resolved
public static void Log(string text)
public static void Log(string text, TraceEventType eventType, TraceEventId id)
{
#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(string.Format("Wait for channel close not successful: {0:G}.", closeWaitResult), 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
11 changes: 6 additions & 5 deletions src/Renci.SshNet/Connection/ConnectorBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Transport;
using System;
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Transport;

namespace Renci.SshNet.Connection
{
Expand Down Expand Up @@ -34,7 +35,7 @@ protected Socket SocketConnect(string host, int port, TimeSpan timeout)
var ipAddress = DnsAbstraction.GetHostAddresses(host)[0];
var ep = new IPEndPoint(ipAddress, port);

DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port));
DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port), TraceEventType.Verbose, TraceEventId.SocketConnection);

var socket = SocketFactory.Create(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

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
5 changes: 4 additions & 1 deletion 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 @@ -191,7 +192,9 @@ public Cipher CreateServerCipher()
Session.ToHex(Session.SessionId),
Session.ConnectionInfo.CurrentServerEncryption,
Session.ToHex(serverKey),
Session.ToHex(serverVector)));
Session.ToHex(serverVector)),
TraceEventType.Verbose,
TraceEventId.ServerCipherCreation);

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

namespace Renci.SshNet
{
Expand Down Expand Up @@ -139,7 +140,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(string.Format("Failed to obtain size of file. Allowing maximum {0} pending reads: {1}", maxPendingReads, ex), TraceEventType.Warning, TraceEventId.FileSizeFetchFailure);
}

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