Faster binary-process log writing

This commit is contained in:
Ben 2025-02-03 10:47:37 +01:00
parent 413a46c761
commit 1e035d6a68
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
2 changed files with 20 additions and 5 deletions

View File

@ -35,6 +35,21 @@ namespace Logging
}
}
public void WriteRawMany(IEnumerable<string> lines)
{
try
{
lock (fileLock)
{
File.AppendAllLines(Filename, lines);
}
}
catch (Exception ex)
{
Console.WriteLine("Writing to log has failed: " + ex);
}
}
private static string GetTimestamp()
{
return $"[{Time.FormatTimestamp(DateTime.UtcNow)}]";

View File

@ -9,7 +9,7 @@ namespace CodexPlugin
private readonly LogFile logFile;
private readonly Process process;
private readonly CodexProcessConfig config;
private readonly List<string> logBuffer = new List<string>();
private List<string> logBuffer = new List<string>();
private readonly object bufferLock = new object();
private readonly List<Task> streamTasks = new List<Task>();
private bool running;
@ -47,14 +47,14 @@ namespace CodexPlugin
{
if (logBuffer.Count > 0)
{
var lines = Array.Empty<string>();
List<string> lines = null!;
lock (bufferLock)
{
lines = logBuffer.ToArray();
logBuffer.Clear();
lines = logBuffer;
logBuffer = new List<string>();
}
foreach (var l in lines) logFile.WriteRaw(l);
logFile.WriteRawMany(lines);
}
else Thread.Sleep(100);
}