From 1e035d6a685b3463f6dc579320aeec13151aabd0 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 3 Feb 2025 10:47:37 +0100 Subject: [PATCH] Faster binary-process log writing --- Framework/Logging/LogFile.cs | 15 +++++++++++++++ .../CodexPlugin/BinaryProcessControl.cs | 10 +++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Framework/Logging/LogFile.cs b/Framework/Logging/LogFile.cs index f9147ce9..2ec94be0 100644 --- a/Framework/Logging/LogFile.cs +++ b/Framework/Logging/LogFile.cs @@ -35,6 +35,21 @@ namespace Logging } } + public void WriteRawMany(IEnumerable 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)}]"; diff --git a/ProjectPlugins/CodexPlugin/BinaryProcessControl.cs b/ProjectPlugins/CodexPlugin/BinaryProcessControl.cs index de8a75c2..c4ed3ee1 100644 --- a/ProjectPlugins/CodexPlugin/BinaryProcessControl.cs +++ b/ProjectPlugins/CodexPlugin/BinaryProcessControl.cs @@ -9,7 +9,7 @@ namespace CodexPlugin private readonly LogFile logFile; private readonly Process process; private readonly CodexProcessConfig config; - private readonly List logBuffer = new List(); + private List logBuffer = new List(); private readonly object bufferLock = new object(); private readonly List streamTasks = new List(); private bool running; @@ -47,14 +47,14 @@ namespace CodexPlugin { if (logBuffer.Count > 0) { - var lines = Array.Empty(); + List lines = null!; lock (bufferLock) { - lines = logBuffer.ToArray(); - logBuffer.Clear(); + lines = logBuffer; + logBuffer = new List(); } - foreach (var l in lines) logFile.WriteRaw(l); + logFile.WriteRawMany(lines); } else Thread.Sleep(100); }