fix: FileLogger thread race

This commit is contained in:
2026-01-28 15:01:59 +03:00
parent 58f307c444
commit 8ff52f7d1e
+13 -3
View File
@@ -5,6 +5,7 @@ public sealed class FileLogger : ILogger
private readonly string _category; private readonly string _category;
private readonly string _filePath; private readonly string _filePath;
private static readonly object _lock = new(); private static readonly object _lock = new();
private Queue<string> _linesToWrite = new Queue<string>();
public FileLogger(string category, string filePath) public FileLogger(string category, string filePath)
{ {
@@ -37,9 +38,18 @@ public sealed class FileLogger : ILogger
lock (_lock) lock (_lock)
{ {
File.AppendAllText(_filePath, line + Environment.NewLine); _linesToWrite.Enqueue(line);
if (exception != null) try
File.AppendAllText(_filePath, exception + Environment.NewLine); {
while (_linesToWrite.Count() > 0)
{
File.AppendAllText(_filePath, _linesToWrite.Dequeue() + Environment.NewLine);
if (exception != null)
File.AppendAllText(_filePath, exception + Environment.NewLine);
}
}
catch
{ }
} }
} }
} }