Add more logging to TorSocks5Client.
MODIFY
WalletWasabi/Logging/Logger.cs
WalletWasabi/Logging/Logger.cs
@@ -233,14 +233,22 @@ namespace WalletWasabi.Logging
#region ExceptionLoggingMethods
- private static void Log(Exception ex, LogLevel level, [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
+ /// <summary>
+ /// Logs user message concatenated with exception string.
+ /// </summary>
+ private static void Log(string message, Exception ex, LogLevel level, [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
{
- Log(level, ExceptionToStringHandleNull(ex), callerFilePath: callerFilePath, callerLineNumber: callerLineNumber);
+ string formattedMessage = message + " Exception: " + (ex?.ToString() ?? "Exception was null.");
+ Log(level, formattedMessage, callerFilePath: callerFilePath, callerLineNumber: callerLineNumber);
}
- private static string ExceptionToStringHandleNull(Exception ex)
+ /// <summary>
+ /// Logs exception string without any user message.
+ /// </summary>
+ private static void Log(Exception ex, LogLevel level, [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
{
- return ex?.ToString() ?? "Exception was null.";
+ string message = ex?.ToString() ?? "Exception was null.";
+ Log(level, message, callerFilePath: callerFilePath, callerLineNumber: callerLineNumber);
}
#endregion ExceptionLoggingMethods
@@ -350,6 +358,14 @@ namespace WalletWasabi.Logging
/// </summary>
public static void LogError(string message, [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1) => Log(LogLevel.Error, message, callerFilePath: callerFilePath, callerLineNumber: callerLineNumber);
+ /// <summary>
+ /// Logs user message with exception concatenated to it at Error level.
+ ///
+ /// For errors and exceptions that cannot be handled.
+ /// These messages indicate a failure in the current activity or operation (such as the current HTTP request), not an application-wide failure.
+ /// Example log message: "Cannot insert record due to duplicate key violation."
+ /// </summary>
+ public static void LogError(string message, Exception ex, [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1) => Log(message, ex, LogLevel.Error, callerFilePath: callerFilePath, callerLineNumber: callerLineNumber);
/// <summary>
/// Logs the <paramref name="ex"/>.ToString() at Error level.
///

MODIFY
WalletWasabi/TorSocks5/TorSocks5Client.cs
WalletWasabi/TorSocks5/TorSocks5Client.cs
@@ -205,11 +205,17 @@ namespace WalletWasabi.TorSocks5
/// <param name="host">IPv4 or domain</param>
internal async Task ConnectToDestinationAsync(string host, int port, bool isRecursiveCall = false)
{
+ Logger.LogDebug($"> {nameof(host)}={host}, {nameof(port)}={port}, {nameof(isRecursiveCall)}={isRecursiveCall}");
+
host = Guard.NotNullOrEmptyOrWhitespace(nameof(host), host, true);
Guard.MinimumAndNotNull(nameof(port), port, 0);
+ try
+ {
if (TorSocks5EndPoint is null)
{
+ Logger.LogDebug($"Tor is NOT enabled.");
+
using (await AsyncLock.LockAsync().ConfigureAwait(false))
{
TcpClient?.Dispose();
@@ -218,11 +224,10 @@ namespace WalletWasabi.TorSocks5
Stream = TcpClient.GetStream();
RemoteEndPoint = TcpClient.Client.RemoteEndPoint;
}
-
- return;
}
-
- var cmd = CmdField.Connect;
+ else
+ {
+ Logger.LogDebug($"Tor is enabled.");
var dstAddr = new AddrField(host);
DestinationHost = dstAddr.DomainOrIPv4;
@@ -230,7 +235,7 @@ namespace WalletWasabi.TorSocks5
var dstPort = new PortField(port);
DestinationPort = dstPort.DstPort;
- var connectionRequest = new TorSocks5Request(cmd, dstAddr, dstPort);
+ var connectionRequest = new TorSocks5Request(cmd: CmdField.Connect, dstAddr, dstPort);
var sendBuffer = connectionRequest.ToBytes();
var receiveBuffer = await SendAsync(sendBuffer, isRecursiveCall: isRecursiveCall).ConfigureAwait(false);
@@ -246,6 +251,7 @@ namespace WalletWasabi.TorSocks5
// the reply.This must be no more than 10 seconds after detecting the
// condition that caused a failure.
DisposeTcpClient();
+ Logger.LogWarning($"Connection response indicates a failure. Actual response is: '{connectionResponse.Rep}'.");
throw new TorSocks5FailureResponseException(connectionResponse.Rep);
}
@@ -261,6 +267,16 @@ namespace WalletWasabi.TorSocks5
// the client, the server MUST encapsulate the data as appropriate for
// the authentication method in use.
}
+ }
+ catch (Exception e)
+ {
+ Logger.LogError("Exception was thrown when connecting to destination.", e);
+ }
+ finally
+ {
+ Logger.LogDebug("<");
+ }
+ }
public async Task AssertConnectedAsync(bool isRecursiveCall = false)
{

Add more logging to TorSocks5Client.