Skip to content

Commit 546caed

Browse files
committed
Include session id in (some) trace messages.
1 parent 4442b88 commit 546caed

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/Renci.SshNet/Session.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,8 @@ public void Connect()
657657
/// </remarks>
658658
public void Disconnect()
659659
{
660+
DiagnosticAbstraction.Log(string.Format("[{0}] Disconnecting session", ToHex(SessionId)));
661+
660662
Disconnect(DisconnectReason.ByApplication, "Connection terminated by the client.");
661663

662664
// at this point, we are sure that the listener thread will stop as we've
@@ -780,7 +782,7 @@ internal void SendMessage(Message message)
780782
WaitOnHandle(_keyExchangeCompletedWaitHandle);
781783
}
782784

783-
DiagnosticAbstraction.Log(string.Format("SendMessage to server '{0}': '{1}'.", message.GetType().Name, message));
785+
DiagnosticAbstraction.Log(string.Format("[{0}] SendMessage to server '{1}': '{2}'.", ToHex(SessionId), message.GetType().Name, message));
784786

785787
// Messages can be sent by different thread so we need to synchronize it
786788
var paddingMultiplier = _clientCipher == null ? (byte)8 : Math.Max((byte)8, _serverCipher.MinimumSize); // Should be recalculate base on cipher min length if cipher specified
@@ -1227,7 +1229,7 @@ private void HandleMessage(ChannelFailureMessage message)
12271229
/// <param name="message"><see cref="DisconnectMessage"/> message.</param>
12281230
protected virtual void OnDisconnectReceived(DisconnectMessage message)
12291231
{
1230-
DiagnosticAbstraction.Log(string.Format("Disconnect received: {0} {1}", message.ReasonCode, message.Description));
1232+
DiagnosticAbstraction.Log(string.Format("[{0}] Disconnect received: {1} {2}", ToHex(SessionId), message.ReasonCode, message.Description));
12311233

12321234
_exception = new SshConnectionException(string.Format(CultureInfo.InvariantCulture, "The connection was closed by the server: {0} ({1}).", message.Description, message.ReasonCode), message.ReasonCode);
12331235
_exceptionWaitHandle.Set();
@@ -1655,11 +1657,11 @@ private Message LoadMessage(byte[] data, int offset)
16551657

16561658
var message = _sshMessageFactory.Create(messageType);
16571659

1658-
DiagnosticAbstraction.Log("Loading message with offset '" + offset + "': " + ToHex(data, 0));
1660+
DiagnosticAbstraction.Log(string.Format("[{0}] Loading message with offset '{1}': {2}", ToHex(SessionId), offset, ToHex(data, 0)));
16591661

16601662
message.Load(data, offset);
16611663

1662-
DiagnosticAbstraction.Log(string.Format("ReceiveMessage from server: '{0}': '{1}'.", message.GetType().Name, message));
1664+
DiagnosticAbstraction.Log(string.Format("[{0}] ReceiveMessage from server: '{1}': '{2}'.", ToHex(SessionId), message.GetType().Name, message));
16631665

16641666
return message;
16651667
}
@@ -1679,6 +1681,14 @@ private static string ToHex(byte[] bytes, int offset)
16791681
return builder.ToString();
16801682
}
16811683

1684+
private static string ToHex(byte[] bytes)
1685+
{
1686+
if (bytes == null)
1687+
return null;
1688+
1689+
return ToHex(bytes, 0);
1690+
}
1691+
16821692
#endregion
16831693

16841694
/// <summary>
@@ -2159,7 +2169,7 @@ private void RaiseError(Exception exp)
21592169
return;
21602170
}
21612171

2162-
DiagnosticAbstraction.Log("Raised exception: " + exp);
2172+
DiagnosticAbstraction.Log(string.Format("[{0}] Raised exception: {1}", ToHex(SessionId), exp));
21632173

21642174
_exception = exp;
21652175

@@ -2220,6 +2230,8 @@ protected virtual void Dispose(bool disposing)
22202230

22212231
if (disposing)
22222232
{
2233+
DiagnosticAbstraction.Log(string.Format("[{0}] Disposing session", ToHex(SessionId)));
2234+
22232235
Disconnect();
22242236

22252237
var serviceAccepted = _serviceAccepted;

0 commit comments

Comments
 (0)