diff --git a/src/Notepads/Services/LoggingService.cs b/src/Notepads/Services/LoggingService.cs index 5c291013d..1d9deaa9b 100644 --- a/src/Notepads/Services/LoggingService.cs +++ b/src/Notepads/Services/LoggingService.cs @@ -13,7 +13,7 @@ public static class LoggingService { - private const string MessageFormat = "{0} [{1}] {2}"; // {timestamp} [{level}] {message} + private const string MessageFormatString = "{0} [{1}] {2}"; // {timestamp} [{level}] {message} private static readonly ConcurrentQueue MessageQueue = new ConcurrentQueue(); private static readonly SemaphoreSlim SemaphoreSlim = new SemaphoreSlim(1, 1); @@ -69,7 +69,8 @@ public static void LogException(Exception ex, bool consoleOnly = false) private static void LogMessage(string level, string message, bool consoleOnly) { - string formattedMessage = string.Format(MessageFormat, DateTime.UtcNow.ToString(CultureInfo.InvariantCulture), level, message); + string timeStamp = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture); + string formattedMessage = string.Format(MessageFormatString, timeStamp, level, message); // Print to console Debug.WriteLine(formattedMessage); @@ -105,21 +106,7 @@ private static async Task InitializeLogFileWriterBackgroundTaskAsync() DateTime.UtcNow.ToString("yyyyMMddTHHmmss", CultureInfo.InvariantCulture) + ".log"); } - _backgroundTask = Task.Run( - async () => - { - while (true) - { - Thread.Sleep(LoggingInterval); - - // We will try to write all pending messages in our next attempt, if the current attempt failed - // However, if the size of messages has become abnormally big, we know something is wrong and should abort at this point - if (!await TryFlushMessageQueueAsync() && Messages.Count > 1000) - { - break; - } - } - }); + _backgroundTask = Task.Run(WriteLogMessages); _initialized = true; LogInfo($"Log file location: {_logFile.Path}", true); @@ -133,10 +120,24 @@ private static async Task InitializeLogFileWriterBackgroundTaskAsync() { SemaphoreSlim.Release(); } - return false; } + private static async Task WriteLogMessages() + { + while (true) + { + Thread.Sleep(LoggingInterval); + + // We will try to write all pending messages in our next attempt, if the current attempt failed + // However, if the size of messages has become abnormally big, we know something is wrong and should abort at this point + if (!await TryFlushMessageQueueAsync() && Messages.Count > 1000) + { + break; + } + } + } + private static async Task TryFlushMessageQueueAsync() { if (!_initialized)