Files
Chronos/src/Chronos.Core/Diagnostics/Log.cs
T
Alby96 e384564784 fix: logging never fails a job, one backup retention, first copy after the upgrade
Owner review 2, phase 2 review findings (scheduler core).

- Log.Write caught only IOException. Microsoft.Extensions.Logging wraps
  provider failures in an AggregateException, so a log line that could not
  be written (in tests: the process-wide sink of a stopped server whose log
  folder was gone) escaped JobEngine.RunAsync's catch before the failure was
  stored: the job stayed running and the worker faulted. This was the cause
  of the intermittent JobApiTests/ArchiveModuleTests failures. Log.Write now
  drops any non-fatal sink failure, TeeLogSink keeps a line from the other
  sinks, FileLogSink drops a line it cannot write and reopens the file at the
  next one, and LoggerSink drops what the providers throw. The scheduler's
  loop relies on the same guarantee.
- The daily backup loop (RunDailyAsync, IsDue, Interval, CheckInterval) had
  no caller since the index-backup schedule replaced it; it is removed and
  the DatabaseBackups summary describes the schedule.
- The first copy after the upgrade no longer waits a day (or for 03:00):
  SystemSchedule.LastDone seeds a new row due at once when its work was last
  done a period or more before, and both hosts pass the newest copy in the
  backups folder for index-backup.
- Back up now (Settings and Archive), the copy kept before a restore and a
  backup job that keeps fewer prune to the largest Copies kept of the backup
  schedules (DatabaseBackups.KeptAsync), not a fixed three. The four
  tooltips that promised three daily copies are reworded (en, it) and their
  stale translations deleted.
- New tests for the scheduler loop itself: a tick, Wake, a pass that threw,
  and dispose during a pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit c5ad0c4c3f636129afe5dd1ce039185516da5621)
2026-10-27 09:56:47 +01:00

98 lines
3.9 KiB
C#

using System.Globalization;
namespace Chronos.Core.Diagnostics;
public enum LogLevel
{
Trace,
Debug,
Info,
Warning,
Error,
}
/// <summary>Destination for engine log events (file logger on desktop, ILogger bridge on the server).</summary>
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. <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).
/// Logging never throws: a line a sink cannot write is dropped, so code that logs from its error handling (the job engine
/// marking a job failed, the scheduler's loop) always goes on to its next step.
/// </summary>
public static class Log
{
private static ILogSink _sink = new ConsoleSink();
public static LogLevel MinimumLevel { get; set; } = LogLevel.Info;
/// <summary>Where lines go now (a test that replaces it puts this one back).</summary>
public static ILogSink Sink => _sink;
public static void SetSink(ILogSink sink) => _sink = sink ?? throw new ArgumentNullException(nameof(sink));
public static void Debug(string category, string message) => Write(LogLevel.Debug, category, message, null);
public static void Info(string category, string message) => Write(LogLevel.Info, category, message, null);
public static void Warning(string category, string message, Exception? ex = null) => Write(LogLevel.Warning, category, message, ex);
public static void Error(string category, string message, Exception? ex = null) => Write(LogLevel.Error, category, message, ex);
public static void Write(LogLevel level, string category, string message, Exception? exception)
{
if (level < MinimumLevel) return;
try
{
_sink.Write(level, category, message, exception);
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
// Logging must never take the process down, nor skip what its caller does next: a log folder removed, a full
// disk or the logger of a host already gone (Microsoft.Extensions.Logging wraps what its providers throw in an
// AggregateException) only lose the line.
}
}
private sealed class ConsoleSink : ILogSink
{
public void Write(LogLevel level, string category, string message, Exception? exception)
{
var line = string.Create(CultureInfo.InvariantCulture, $"{DateTime.UtcNow:O} {level,-7} {category}: {message}");
if (exception is not null) line += Environment.NewLine + exception;
Console.Error.WriteLine(line);
}
}
}
/// <summary>Writes every event to several sinks; one that cannot write it does not keep it from the others.</summary>
public sealed class TeeLogSink(params ILogSink[] sinks) : ILogSink
{
public void Write(LogLevel level, string category, string message, Exception? exception)
{
foreach (var sink in sinks)
{
try
{
sink.Write(level, category, message, exception);
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
// That sink loses the line (see Log.Write).
}
}
}
}
/// <summary>Writes events to standard error (CI and command-line diagnostics).</summary>
public sealed class StderrLogSink : ILogSink
{
public void Write(LogLevel level, string category, string message, Exception? exception)
{
var line = string.Create(CultureInfo.InvariantCulture, $"{DateTime.UtcNow:O} {level,-7} {category}: {message}");
if (exception is not null) line += Environment.NewLine + exception;
Console.Error.WriteLine(line);
}
}