feat(logs): log level, retention, size limits and rotation on server and desktop

Owner review 2, R11 asked for more log settings. The file sink now writes daily or
hourly files, continues a full file in numbered parts and prunes by age and by the
total size (never the file being written); its options change while it runs.

Server: the file and console levels are separate and read by the logging filters
at every line, so the admin panel's new Logs group applies without a restart; the
engine's level follows the lower of the two. This also fixes a bug: the file logger
was fixed at Information, so CHRONOS_LOG_LEVEL=Debug or Trace never reached the
files. New variables CHRONOS_LOG_CONSOLE_LEVEL, CHRONOS_LOG_RETENTION_DAYS,
CHRONOS_LOG_MAX_FILE_MB, CHRONOS_LOG_MAX_TOTAL_MB, CHRONOS_LOG_ROTATION,
CHRONOS_AUDIT_RETENTION_DAYS and CHRONOS_DIAGNOSTICS_LOG_MB lock their settings like
the others; the diagnostics zip keeps the configured share of each log.

Desktop: Settings > Advanced gains Log level, Keep logs, File size, Logs total,
Rotation and Diagnostics logs, applied at every save; Advanced's Reset section
restores them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit cc875784e4952ee0e4c807b9d9c18496d3d5a8c3)
This commit is contained in:
Alby96 committed 2026-10-27 02:08:09 +01:00
1 parent 1f089888e8
commit 0e84392f4f
35 files changed
+1485 -119

No files matched your search

+5 -61
View File
@@ -17,7 +17,11 @@ public interface ILogSink
void Write(LogLevel level, string category, string message, Exception? exception);
}
/// <summary>Tiny engine-wide logger. Core code logs through it; hosts plug in a sink.</summary>
/// <summary>
/// Tiny engine-wide logger. Core code logs through it; hosts plug in a sink. <see cref="MinimumLevel"/> drops lines no sink
/// wants before they are formatted: hosts set it to the lowest level of their sinks, which each keep their own (a
/// <see cref="FileLogSink"/> its <see cref="LogFileOptions.Level"/>, the server's console and files their filters).
/// </summary>
public static class Log
{
private static ILogSink _sink = new ConsoleSink();
@@ -55,66 +59,6 @@ public static class Log
}
}
/// <summary>Appends log lines to rotating files (one per day, kept for <c>retainDays</c>).</summary>
public sealed class FileLogSink : ILogSink, IDisposable
{
private readonly string _directory;
private readonly string _prefix;
private readonly int _retainDays;
private readonly Lock _gate = new();
private StreamWriter? _writer;
private DateOnly _day;
public FileLogSink(string directory, string prefix, int retainDays = 14)
{
_directory = directory;
_prefix = prefix;
_retainDays = retainDays;
Directory.CreateDirectory(directory);
}
public string CurrentPath => Path.Combine(_directory, $"{_prefix}-{_day:yyyyMMdd}.log");
public void Write(LogLevel level, string category, string message, Exception? exception)
{
lock (_gate)
{
var today = DateOnly.FromDateTime(DateTime.UtcNow);
if (_writer is null || today != _day)
{
_writer?.Dispose();
_day = today;
_writer = new StreamWriter(new FileStream(CurrentPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)) { AutoFlush = true };
Prune();
}
_writer.WriteLine(string.Create(CultureInfo.InvariantCulture, $"{DateTime.UtcNow:O} {level,-7} {category}: {message}"));
if (exception is not null) _writer.WriteLine(exception.ToString());
}
}
private void Prune()
{
foreach (var file in Directory.EnumerateFiles(_directory, _prefix + "-*.log"))
{
if (File.GetLastWriteTimeUtc(file) < DateTime.UtcNow.AddDays(-_retainDays))
{
try
{
File.Delete(file);
}
catch (IOException)
{
}
}
}
}
public void Dispose()
{
lock (_gate) _writer?.Dispose();
}
}
/// <summary>Writes every event to several sinks.</summary>
public sealed class TeeLogSink(params ILogSink[] sinks) : ILogSink
{