Remove code duplication in logging and refactor it a bit.
MODIFY
WalletWasabi/Logging/Logger.cs
WalletWasabi/Logging/Logger.cs
@@ -119,7 +119,7 @@ namespace WalletWasabi.Logging
#region GeneralLoggingMethods
- private static void Log(LogLevel level, string message, string category, int newLinesBefore = 0, bool insertNewLinesLogFileOnlyMode = true)
+ private static void Log(LogLevel level, string message, string category, int additionalEntrySeparators = 0, bool additionalEntrySeparatorsLogFileOnlyMode = true)
{
try
{
@@ -137,33 +137,34 @@ namespace WalletWasabi.Logging
category = string.IsNullOrWhiteSpace(category) ? "" : category;
var messageBuilder = new StringBuilder();
+ messageBuilder.Append($"{DateTime.UtcNow.ToLocalTime():yyyy-MM-dd HH:mm:ss} {level.ToString().ToUpperInvariant()}");
if (message != "" && category != "") // If none of them empty.
{
- messageBuilder.Append($"{DateTime.UtcNow.ToLocalTime():yyyy-MM-dd HH:mm:ss} {level.ToString().ToUpperInvariant()} {category}: {message}{EntrySeparator}");
+ messageBuilder.Append($" {category}: {message}{EntrySeparator}");
}
else if (message == "" && category != "") // If only the message is empty.
{
- messageBuilder.Append($"{DateTime.UtcNow.ToLocalTime():yyyy-MM-dd HH:mm:ss} {level.ToString().ToUpperInvariant()} {category}{EntrySeparator}");
+ messageBuilder.Append($" {category}{EntrySeparator}");
}
else if (message != "" && category == "") // If only the category is empty.
{
- messageBuilder.Append($"{DateTime.UtcNow.ToLocalTime():yyyy-MM-dd HH:mm:ss} {level.ToString().ToUpperInvariant()}: {message}{EntrySeparator}");
+ messageBuilder.Append($": {message}{EntrySeparator}");
}
else // if (message == "" && category == "") // If both empty. It probably never happens though.
{
- messageBuilder.Append($"{DateTime.UtcNow.ToLocalTime():yyyy-MM-dd HH:mm:ss} {level.ToString().ToUpperInvariant()}{EntrySeparator}");
+ messageBuilder.Append($"{EntrySeparator}");
}
var finalMessage = messageBuilder.ToString();
- for (int i = 0; i < newLinesBefore; i++)
+ for (int i = 0; i < additionalEntrySeparators; i++)
{
- messageBuilder.Insert(0, Environment.NewLine);
+ messageBuilder.Insert(0, EntrySeparator);
}
var finalFileMessage = messageBuilder.ToString();
- if (!insertNewLinesLogFileOnlyMode)
+ if (!additionalEntrySeparatorsLogFileOnlyMode)
{
finalMessage = finalFileMessage;
}
@@ -389,7 +390,7 @@ namespace WalletWasabi.Logging
/// Logs software start with category InstanceGuid and insert three newlines.
/// </summary>
/// <param name="appName">The name of the app.</param>
- public static void LogStarting(string appName) => Log(LogLevel.Info, $"{appName} is starting...", category: InstanceGuid.ToString(), newLinesBefore: 3, insertNewLinesLogFileOnlyMode: true);
+ public static void LogStarting(string appName) => Log(LogLevel.Info, $"{appName} is starting...", category: InstanceGuid.ToString(), additionalEntrySeparators: 3, additionalEntrySeparatorsLogFileOnlyMode: true);
/// <summary>
/// For tracking the general flow of the application.

Remove code duplication in logging and refactor it a bit.