From 8ff52f7d1e714112c3cf4fe6974cc11f797c95a5 Mon Sep 17 00:00:00 2001 From: Alek-ban Date: Wed, 28 Jan 2026 15:01:59 +0300 Subject: [PATCH] fix: FileLogger thread race --- GSS2.Test/Logging/FileLogger.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/GSS2.Test/Logging/FileLogger.cs b/GSS2.Test/Logging/FileLogger.cs index c2067bf..09ca46f 100644 --- a/GSS2.Test/Logging/FileLogger.cs +++ b/GSS2.Test/Logging/FileLogger.cs @@ -5,6 +5,7 @@ public sealed class FileLogger : ILogger private readonly string _category; private readonly string _filePath; private static readonly object _lock = new(); + private Queue _linesToWrite = new Queue(); public FileLogger(string category, string filePath) { @@ -37,9 +38,18 @@ public sealed class FileLogger : ILogger lock (_lock) { - File.AppendAllText(_filePath, line + Environment.NewLine); - if (exception != null) - File.AppendAllText(_filePath, exception + Environment.NewLine); + _linesToWrite.Enqueue(line); + try + { + while (_linesToWrite.Count() > 0) + { + File.AppendAllText(_filePath, _linesToWrite.Dequeue() + Environment.NewLine); + if (exception != null) + File.AppendAllText(_filePath, exception + Environment.NewLine); + } + } + catch + { } } } }