Skip to content

Commit cf4026a

Browse files
committed
Enable diagnostics in release
1 parent 6d9d032 commit cf4026a

File tree

17 files changed

+248
-101
lines changed

17 files changed

+248
-101
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,17 @@ MA0053.exceptions_should_be_sealed = true
571571
# Also report diagnostic for types that define (new) virtual members.
572572
MA0053.class_with_virtual_member_shoud_be_sealed = true
573573

574+
# MA0076: Do not use implicit culture-sensitive ToString in interpolated strings
575+
#
576+
# Interpolated strings have performance benefits.
577+
dotnet_diagnostic.MA0076.severity = none
578+
579+
# MA0089: Optimize string method usage
580+
#
581+
# Duplicate of CA1865-CA1867 /
582+
# For string.Join, results in verbose code for very little gain.
583+
dotnet_diagnostic.MA0089.severity = none
584+
574585
# MA0112: Use 'Count > 0' instead of 'Any()'
575586
#
576587
# This rule is disabled by default, hence we need to explicitly enable it.

src/Renci.SshNet/BaseClient.cs

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

6-
using Renci.SshNet.Abstractions;
76
using Renci.SshNet.Common;
87
using Renci.SshNet.Messages.Transport;
98

@@ -323,7 +322,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
323322
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
324323
public void Disconnect()
325324
{
326-
DiagnosticAbstraction.Log("Disconnecting client.");
325+
Diagnostics.Log("Disconnecting client.", System.Diagnostics.TraceEventType.Verbose);
327326

328327
CheckDisposed();
329328

@@ -422,7 +421,7 @@ protected virtual void Dispose(bool disposing)
422421

423422
if (disposing)
424423
{
425-
DiagnosticAbstraction.Log("Disposing client.");
424+
Diagnostics.Log("Disposing client.", System.Diagnostics.TraceEventType.Verbose);
426425

427426
Disconnect();
428427

src/Renci.SshNet/Channels/Channel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Globalization;
34
using System.Net.Sockets;
45
using System.Threading;
56

6-
using Renci.SshNet.Abstractions;
77
using Renci.SshNet.Common;
88
using Renci.SshNet.Messages;
99
using Renci.SshNet.Messages.Connection;
@@ -554,9 +554,9 @@ protected virtual void Close()
554554
// SSH_MSG_CHANNEL_CLOSE as response to a SSH_MSG_CHANNEL_CLOSE sent by the
555555
// server
556556
var closeWaitResult = _session.TryWait(_channelClosedWaitHandle, ConnectionInfo.ChannelCloseTimeout);
557-
if (closeWaitResult != WaitResult.Success)
557+
if (closeWaitResult != WaitResult.Success && Diagnostics.IsEnabled(TraceEventType.Warning))
558558
{
559-
DiagnosticAbstraction.Log(string.Format("Wait for channel close not successful: {0:G}.", closeWaitResult));
559+
Diagnostics.Log($"Wait for channel close not successful: {closeWaitResult}.", TraceEventType.Warning);
560560
}
561561
}
562562
}

src/Renci.SshNet/Channels/ChannelDirectTcpip.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Net;
34
using System.Net.Sockets;
45
using System.Threading;
6+
57
using Renci.SshNet.Abstractions;
68
using Renci.SshNet.Common;
79
using Renci.SshNet.Messages.Connection;
@@ -156,8 +158,10 @@ private void ShutdownSocket(SocketShutdown how)
156158
}
157159
catch (SocketException ex)
158160
{
159-
// TODO: log as warning
160-
DiagnosticAbstraction.Log("Failure shutting down socket: " + ex);
161+
if (Diagnostics.IsEnabled(TraceEventType.Warning))
162+
{
163+
Diagnostics.Log("Failure shutting down socket: " + ex, TraceEventType.Warning);
164+
}
161165
}
162166
}
163167
}

src/Renci.SshNet/Channels/ChannelForwardedTcpip.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Net;
34
using System.Net.Sockets;
5+
46
using Renci.SshNet.Abstractions;
57
using Renci.SshNet.Common;
68
using Renci.SshNet.Messages.Connection;
@@ -138,8 +140,10 @@ private void ShutdownSocket(SocketShutdown how)
138140
}
139141
catch (SocketException ex)
140142
{
141-
// TODO: log as warning
142-
DiagnosticAbstraction.Log("Failure shutting down socket: " + ex);
143+
if (Diagnostics.IsEnabled(TraceEventType.Warning))
144+
{
145+
Diagnostics.Log("Failure shutting down socket: " + ex, TraceEventType.Warning);
146+
}
143147
}
144148
}
145149
}

src/Renci.SshNet/Connection/ConnectorBase.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Net;
34
using System.Net.Sockets;
45
using System.Threading;
@@ -41,7 +42,10 @@ protected Socket SocketConnect(string host, int port, TimeSpan timeout)
4142
var ipAddress = DnsAbstraction.GetHostAddresses(host)[0];
4243
var ep = new IPEndPoint(ipAddress, port);
4344

44-
DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port));
45+
if (Diagnostics.IsEnabled(TraceEventType.Information))
46+
{
47+
Diagnostics.Log($"Initiating connection to '{host}:{port}'.", TraceEventType.Information);
48+
}
4549

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

@@ -76,7 +80,10 @@ protected async Task<Socket> SocketConnectAsync(string host, int port, Cancellat
7680
var ipAddress = (await DnsAbstraction.GetHostAddressesAsync(host).ConfigureAwait(false))[0];
7781
var ep = new IPEndPoint(ipAddress, port);
7882

79-
DiagnosticAbstraction.Log(string.Format("Initiating connection to '{0}:{1}'.", host, port));
83+
if (Diagnostics.IsEnabled(TraceEventType.Information))
84+
{
85+
Diagnostics.Log($"Initiating connection to '{host}:{port}'.", TraceEventType.Information);
86+
}
8087

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

src/Renci.SshNet/Abstractions/DiagnosticAbstraction.cs renamed to src/Renci.SshNet/Diagnostics.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
using System.ComponentModel;
1+
using System;
22
using System.Diagnostics;
33

4-
namespace Renci.SshNet.Abstractions
4+
namespace Renci.SshNet
55
{
66
/// <summary>
77
/// Provides access to the <see cref="System.Diagnostics"/> internals of SSH.NET.
88
/// </summary>
9-
[EditorBrowsable(EditorBrowsableState.Never)]
10-
public static class DiagnosticAbstraction
9+
public static class Diagnostics
1110
{
1211
/// <summary>
1312
/// The <see cref="TraceSource"/> instance used by SSH.NET.
1413
/// </summary>
1514
/// <remarks>
1615
/// <para>
17-
/// Currently, the library only traces events when compiled in Debug mode.
18-
/// </para>
19-
/// <para>
2016
/// Configuration on .NET Core must be done programmatically, e.g.
2117
/// <code>
22-
/// DiagnosticAbstraction.Source.Switch = new SourceSwitch("sourceSwitch", "Verbose");
23-
/// DiagnosticAbstraction.Source.Listeners.Remove("Default");
24-
/// DiagnosticAbstraction.Source.Listeners.Add(new ConsoleTraceListener());
25-
/// DiagnosticAbstraction.Source.Listeners.Add(new TextWriterTraceListener("trace.log"));
18+
/// Diagnostics.Source.Switch = new SourceSwitch("sourceSwitch", nameof(SourceLevels.Verbose));
19+
/// Diagnostics.Source.Listeners.Remove("Default");
20+
/// Diagnostics.Source.Listeners.Add(new ConsoleTraceListener());
21+
/// Diagnostics.Source.Listeners.Add(new TextWriterTraceListener("SshNetTrace.log"));
2622
/// </code>
2723
/// </para>
2824
/// <para>
@@ -53,17 +49,19 @@ public static class DiagnosticAbstraction
5349
public static readonly TraceSource Source = new TraceSource("SshNet.Logging");
5450

5551
/// <summary>
56-
/// Logs a message to <see cref="Source"/> at the <see cref="TraceEventType.Verbose"/>
57-
/// level.
52+
/// Logs a message to <see cref="Source"/> with the specified event type.
5853
/// </summary>
5954
/// <param name="text">The message to log.</param>
60-
/// <param name="type">The trace event type.</param>
61-
[Conditional("DEBUG")]
62-
public static void Log(string text, TraceEventType type = TraceEventType.Verbose)
55+
/// <param name="eventType">The trace event type.</param>
56+
public static void Log(string text, TraceEventType eventType)
57+
{
58+
Source.TraceEvent(eventType, Environment.CurrentManagedThreadId, text);
59+
}
60+
61+
/// <inheritdoc cref="SourceSwitch.ShouldTrace(TraceEventType)" />
62+
public static bool IsEnabled(TraceEventType eventType)
6363
{
64-
Source.TraceEvent(type,
65-
System.Environment.CurrentManagedThreadId,
66-
text);
64+
return Source.Switch.ShouldTrace(eventType);
6765
}
6866
}
6967
}

src/Renci.SshNet/ForwardedPortDynamic.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Globalization;
34
using System.Linq;
45
using System.Net;
@@ -413,8 +414,7 @@ private void InternalStop(TimeSpan timeout)
413414

414415
if (!_pendingChannelCountdown.Wait(timeout))
415416
{
416-
// TODO: log as warning
417-
DiagnosticAbstraction.Log("Timeout waiting for pending channels in dynamic forwarded port to close.");
417+
Diagnostics.Log("Timeout waiting for pending channels in dynamic forwarded port to close.", TraceEventType.Warning);
418418
}
419419
}
420420

src/Renci.SshNet/ForwardedPortLocal.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Net;
34
using System.Net.Sockets;
45
using System.Threading;
@@ -402,8 +403,7 @@ private void InternalStop(TimeSpan timeout)
402403

403404
if (!_pendingChannelCountdown.Wait(timeout))
404405
{
405-
// TODO: log as warning
406-
DiagnosticAbstraction.Log("Timeout waiting for pending channels in local forwarded port to close.");
406+
Diagnostics.Log("Timeout waiting for pending channels in local forwarded port to close.", TraceEventType.Warning);
407407
}
408408
}
409409

src/Renci.SshNet/ForwardedPortRemote.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Globalization;
34
using System.Net;
45
using System.Threading;
@@ -216,8 +217,7 @@ protected override void StopPort(TimeSpan timeout)
216217

217218
if (!_pendingChannelCountdown.Wait(timeout))
218219
{
219-
// TODO: log as warning
220-
DiagnosticAbstraction.Log("Timeout waiting for pending channels in remote forwarded port to close.");
220+
Diagnostics.Log("Timeout waiting for pending channels in remote forwarded port to close.", TraceEventType.Warning);
221221
}
222222

223223
_status = ForwardedPortStatus.Stopped;

src/Renci.SshNet/Messages/Transport/DisconnectMessage.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,11 @@ internal override void Process(Session session)
9595
{
9696
session.OnDisconnectReceived(this);
9797
}
98+
99+
/// <inheritdoc/>
100+
public override string ToString()
101+
{
102+
return $"SSH_MSG_DISCONNECT ({ReasonCode}) {Description}";
103+
}
98104
}
99105
}

0 commit comments

Comments
 (0)