fix: file logger having async issues
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Engine.Core.Debug;
|
namespace Engine.Core.Debug;
|
||||||
|
|
||||||
@@ -7,9 +9,21 @@ public class FileLogger : LoggerBase
|
|||||||
{
|
{
|
||||||
public readonly string FilePath;
|
public readonly string FilePath;
|
||||||
|
|
||||||
protected override void Write(string message)
|
private readonly Queue<string> lineQueue = new();
|
||||||
|
private Task? currentWriteTask = null;
|
||||||
|
|
||||||
|
protected async override void Write(string message)
|
||||||
{
|
{
|
||||||
File.AppendAllTextAsync(FilePath, $"{message}{Environment.NewLine}");
|
lineQueue.Enqueue(message);
|
||||||
|
currentWriteTask ??= WriteLogs();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task WriteLogs()
|
||||||
|
{
|
||||||
|
while (lineQueue.TryDequeue(out string? line))
|
||||||
|
await File.AppendAllTextAsync(FilePath, $"{line}{Environment.NewLine}");
|
||||||
|
|
||||||
|
currentWriteTask = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileLogger(string filePath)
|
public FileLogger(string filePath)
|
||||||
|
|||||||
Reference in New Issue
Block a user