forked from amkovkov/GranuSightSoftware2
fix: FileLogger thread race
This commit is contained in:
@@ -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
|
||||||
|
{ }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user