From d2bc586dab98129852b76bc1a80eced59d06c8f6 Mon Sep 17 00:00:00 2001 From: hishitetsu <66369541+hishitetsu@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:29:48 +0900 Subject: [PATCH 1/3] Update FileLogger.cs --- src/Files.Shared/Utils/Logger/FileLogger.cs | 52 +++++++++------------ 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/src/Files.Shared/Utils/Logger/FileLogger.cs b/src/Files.Shared/Utils/Logger/FileLogger.cs index 5e4a5d52ca95..09abf9aed7d7 100644 --- a/src/Files.Shared/Utils/Logger/FileLogger.cs +++ b/src/Files.Shared/Utils/Logger/FileLogger.cs @@ -6,13 +6,12 @@ using System.Diagnostics; using System.IO; using System.Linq; -using System.Threading; namespace Files.Shared { public sealed class FileLogger : ILogger { - private readonly SemaphoreSlim semaphoreSlim = new(1); + private readonly object syncRoot = new(); private readonly string filePath; public FileLogger(string filePath) @@ -34,21 +33,19 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except { if (formatter is null) return; - semaphoreSlim.Wait(); - try + lock (syncRoot) { - var message = exception?.ToString() ?? formatter(state, exception); + try + { + var message = exception?.ToString() ?? formatter(state, exception); - File.AppendAllText(filePath, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}|{logLevel}|{message}" + Environment.NewLine); - } - catch (Exception e) - { - Debug.WriteLine($"Writing to log file failed with the following exception:\n{e}"); - } - finally - { - semaphoreSlim.Release(); + File.AppendAllText(filePath, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}|{logLevel}|{message}" + Environment.NewLine); + } + catch (Exception e) + { + Debug.WriteLine($"Writing to log file failed with the following exception:\n{e}"); + } } } @@ -57,24 +54,21 @@ public void PurgeLogs(int numberOfLinesKept) if (!File.Exists(filePath)) return; - semaphoreSlim.Wait(); - - try + lock (syncRoot) { - var lines = File.ReadAllLines(filePath); - if (lines.Length > numberOfLinesKept) + try { - var lastLines = lines.Skip(Math.Max(0, lines.Length - numberOfLinesKept)); - File.WriteAllLines(filePath, lastLines); + var lines = File.ReadAllLines(filePath); + if (lines.Length > numberOfLinesKept) + { + var lastLines = lines.Skip(Math.Max(0, lines.Length - numberOfLinesKept)); + File.WriteAllLines(filePath, lastLines); + } + } + catch (Exception e) + { + Debug.WriteLine($"Purging the log file failed with the following exception:\n{e}"); } - } - catch (Exception e) - { - Debug.WriteLine($"Purging the log file failed with the following exception:\n{e}"); - } - finally - { - semaphoreSlim.Release(); } } } From 07ef1be10ddb597abe8f452c00bc9ef58b98d300 Mon Sep 17 00:00:00 2001 From: hishitetsu <66369541+hishitetsu@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:39:38 +0900 Subject: [PATCH 2/3] Update FileLogger.cs --- src/Files.Shared/Utils/Logger/FileLogger.cs | 28 ++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Files.Shared/Utils/Logger/FileLogger.cs b/src/Files.Shared/Utils/Logger/FileLogger.cs index 09abf9aed7d7..4e398c334ae1 100644 --- a/src/Files.Shared/Utils/Logger/FileLogger.cs +++ b/src/Files.Shared/Utils/Logger/FileLogger.cs @@ -34,18 +34,18 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except if (formatter is null) return; - lock (syncRoot) + try { - try + var message = exception?.ToString() ?? formatter(state, exception); + + lock (syncRoot) { - var message = exception?.ToString() ?? formatter(state, exception); - File.AppendAllText(filePath, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}|{logLevel}|{message}" + Environment.NewLine); } - catch (Exception e) - { - Debug.WriteLine($"Writing to log file failed with the following exception:\n{e}"); - } + } + catch (Exception e) + { + Debug.WriteLine($"Writing to log file failed with the following exception:\n{e}"); } } @@ -54,9 +54,9 @@ public void PurgeLogs(int numberOfLinesKept) if (!File.Exists(filePath)) return; - lock (syncRoot) + try { - try + lock (syncRoot) { var lines = File.ReadAllLines(filePath); if (lines.Length > numberOfLinesKept) @@ -65,10 +65,10 @@ public void PurgeLogs(int numberOfLinesKept) File.WriteAllLines(filePath, lastLines); } } - catch (Exception e) - { - Debug.WriteLine($"Purging the log file failed with the following exception:\n{e}"); - } + } + catch (Exception e) + { + Debug.WriteLine($"Purging the log file failed with the following exception:\n{e}"); } } } From 39ad46bf5efed9df80a6e2df830b9fd318ca3fc5 Mon Sep 17 00:00:00 2001 From: hishitetsu <66369541+hishitetsu@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:42:33 +0900 Subject: [PATCH 3/3] Update FileLogger.cs --- src/Files.Shared/Utils/Logger/FileLogger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Files.Shared/Utils/Logger/FileLogger.cs b/src/Files.Shared/Utils/Logger/FileLogger.cs index 4e398c334ae1..102ee94c15e9 100644 --- a/src/Files.Shared/Utils/Logger/FileLogger.cs +++ b/src/Files.Shared/Utils/Logger/FileLogger.cs @@ -37,7 +37,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except try { var message = exception?.ToString() ?? formatter(state, exception); - + lock (syncRoot) { File.AppendAllText(filePath, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff}|{logLevel}|{message}" + Environment.NewLine);