Implement and use BenchmarkLogger
ADD
WalletWasabi/Logging/BenchmarkLogger.cs
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using System.Text;
+
+namespace WalletWasabi.Logging
+{
+ public class BenchmarkLogger : IDisposable
+ {
+ private LogLevel LogLevel { get; }
+ public DateTimeOffset InitStart { get; }
+
+ public string OperationName { get; }
+ public string CallerFilePath { get; }
+ public int CallerLineNumber { get; }
+
+ private BenchmarkLogger(LogLevel logLevel = LogLevel.Info, [CallerMemberName]string operationName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
+ {
+ LogLevel = logLevel;
+ InitStart = DateTimeOffset.UtcNow;
+ OperationName = operationName;
+ CallerFilePath = callerFilePath;
+ CallerLineNumber = callerLineNumber;
+ }
+
+ /// <summary>
+ /// Logs the time between the creation of the class and the disposing of the class.
+ /// Example usage: using(BenchmarkLogger.Measure()){}
+ /// </summary>
+ /// <param name="operationName">Which operation to measure. Default is the caller function name.</param>
+ public static IDisposable Measure(LogLevel logLevel = LogLevel.Info, [CallerMemberName]string operationName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1)
+ {
+ return new BenchmarkLogger(logLevel, operationName, callerFilePath, callerLineNumber);
+ }
+
+ #region IDisposable Support
+
+ private bool _disposedValue = false; // To detect redundant calls
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (!_disposedValue)
+ {
+ if (disposing)
+ {
+ var elapsedSeconds = Math.Round((DateTimeOffset.UtcNow - InitStart).TotalSeconds, 1);
+ string message = $"{OperationName} finished in {elapsedSeconds} seconds.";
+
+ switch (LogLevel)
+ {
+ case LogLevel.Trace:
+ Logger.LogTrace(message, CallerFilePath, CallerLineNumber);
+ break;
+
+ case LogLevel.Debug:
+ Logger.LogDebug(message, CallerFilePath, CallerLineNumber);
+ break;
+
+ case LogLevel.Info:
+ Logger.LogInfo(message, CallerFilePath, CallerLineNumber);
+ break;
+
+ case LogLevel.Warning:
+ Logger.LogWarning(message, CallerFilePath, CallerLineNumber);
+ break;
+
+ case LogLevel.Error:
+ Logger.LogError(message, CallerFilePath, CallerLineNumber);
+ break;
+
+ case LogLevel.Critical:
+ Logger.LogCritical(message, CallerFilePath, CallerLineNumber);
+ break;
+
+ default:
+ Logger.LogInfo(message, CallerFilePath, CallerLineNumber);
+ break;
+ }
+ }
+
+ _disposedValue = true;
+ }
+ }
+
+ // This code added to correctly implement the disposable pattern.
+ public void Dispose()
+ {
+ // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
+ Dispose(true);
+ }
+
+ #endregion IDisposable Support
+ }
+}

MODIFY
WalletWasabi/Stores/BitcoinStore.cs
WalletWasabi/Stores/BitcoinStore.cs
@@ -5,6 +5,7 @@ using System.IO;
using System.Text;
using System.Threading.Tasks;
using WalletWasabi.Helpers;
+using WalletWasabi.Logging;
namespace WalletWasabi.Stores
{
@@ -21,6 +22,8 @@ namespace WalletWasabi.Stores
public HashChain HashChain { get; private set; }
public async Task InitializeAsync(string workFolderPath, Network network)
+ {
+ using (BenchmarkLogger.Measure())
{
WorkFolderPath = Guard.NotNullOrEmptyOrWhitespace(nameof(workFolderPath), workFolderPath, trim: true);
IoHelpers.EnsureDirectoryExists(WorkFolderPath);
@@ -34,3 +37,4 @@ namespace WalletWasabi.Stores
}
}
}
+}

MODIFY
WalletWasabi/Stores/IndexStore.cs
WalletWasabi/Stores/IndexStore.cs
@@ -37,6 +37,8 @@ namespace WalletWasabi.Stores
public event EventHandler<FilterModel> NewFilter;
public async Task InitializeAsync(string workFolderPath, Network network, HashChain hashChain)
+ {
+ using (BenchmarkLogger.Measure())
{
WorkFolderPath = Guard.NotNullOrEmptyOrWhitespace(nameof(workFolderPath), workFolderPath, trim: true);
Network = Guard.NotNull(nameof(network), network);
@@ -75,6 +77,7 @@ namespace WalletWasabi.Stores
await InitializeFiltersAsync();
}
}
+ }
private async Task InitializeFiltersAsync()
{

Implement and use BenchmarkLogger