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)
126 lines
6.3 KiB
C#
126 lines
6.3 KiB
C#
using System.Globalization;
|
||
using Chronos.App.Infrastructure;
|
||
using Chronos.App.Settings;
|
||
using Chronos.Core.Diagnostics;
|
||
using Chronos.Core.Localization;
|
||
using Chronos.Testing;
|
||
|
||
namespace Chronos.App.Tests.Settings;
|
||
|
||
/// <summary>Settings › Advanced › Logs (owner review 2, R11): the stored values, the file options they become and the rows.</summary>
|
||
public sealed class LogSettingsTests
|
||
{
|
||
private readonly TestContext _context;
|
||
|
||
public LogSettingsTests(TestContext context)
|
||
{
|
||
_context = context;
|
||
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
|
||
}
|
||
|
||
[Test]
|
||
public void LogSettingsSurviveTheSettingsFileAndOlderFilesGetTheDefaults()
|
||
{
|
||
var settings = new AppSettings
|
||
{
|
||
LogLevel = LogLevel.Debug,
|
||
LogRetentionDays = 30,
|
||
LogMaxFileMb = 0,
|
||
LogMaxTotalMb = 2048,
|
||
LogRotation = LogRotation.Hourly,
|
||
DiagnosticsLogMb = 16,
|
||
};
|
||
var json = settings.ToJson();
|
||
Assert.Contains("\"LogLevel\": \"Debug\"", json);
|
||
Assert.Contains("\"LogRotation\": \"Hourly\"", json);
|
||
foreach (var copy in new[] { AppSettings.Parse(json)!, settings.Clone() })
|
||
{
|
||
Assert.Equal((LogLevel.Debug, 30, 0, 2048, LogRotation.Hourly, 16),
|
||
(copy.LogLevel, copy.LogRetentionDays, copy.LogMaxFileMb, copy.LogMaxTotalMb, copy.LogRotation, copy.DiagnosticsLogMb));
|
||
}
|
||
|
||
// A settings file written before these rows existed (or exported by such a copy) keeps the former behaviour's defaults.
|
||
var older = AppSettings.Parse("""{ "Theme": "Light" }""")!;
|
||
Assert.Equal((LogLevel.Info, 14, 50, 1024, LogRotation.Daily, 4),
|
||
(older.LogLevel, older.LogRetentionDays, older.LogMaxFileMb, older.LogMaxTotalMb, older.LogRotation, older.DiagnosticsLogMb));
|
||
}
|
||
|
||
[Test]
|
||
public void TheSettingsBecomeTheLogFileOptions()
|
||
{
|
||
Assert.Equal(new LogFileOptions(), AppLogging.Options(new AppSettings()));
|
||
Assert.Equal(4L << 20, AppLogging.DiagnosticsTailBytes(new AppSettings()));
|
||
var tuned = new AppSettings { LogLevel = LogLevel.Warning, LogRetentionDays = 3, LogMaxFileMb = 10, LogMaxTotalMb = 0, LogRotation = LogRotation.Hourly };
|
||
Assert.Equal(new LogFileOptions { Level = LogLevel.Warning, RetentionDays = 3, MaxFileMb = 10, MaxTotalMb = 0, Rotation = LogRotation.Hourly },
|
||
AppLogging.Options(tuned));
|
||
|
||
// A hand-edited file never makes the sink keep nothing or delete everything.
|
||
var odd = new AppSettings { LogLevel = (LogLevel)42, LogRetentionDays = 0, LogMaxFileMb = -3, LogMaxTotalMb = -1, LogRotation = (LogRotation)7, DiagnosticsLogMb = 0 };
|
||
Assert.Equal(new LogFileOptions { RetentionDays = 1, MaxFileMb = 0, MaxTotalMb = 0 }, AppLogging.Options(odd));
|
||
Assert.Equal(1L << 20, AppLogging.DiagnosticsTailBytes(odd));
|
||
|
||
var level = Log.MinimumLevel;
|
||
using var sink = new FileLogSink(_context.CreateTempDirectory(), "chronos");
|
||
try
|
||
{
|
||
AppLogging.Apply(sink, tuned);
|
||
Assert.Equal(AppLogging.Options(tuned), sink.Options);
|
||
Assert.Equal(LogLevel.Warning, Log.MinimumLevel);
|
||
}
|
||
finally
|
||
{
|
||
Log.MinimumLevel = level;
|
||
}
|
||
}
|
||
|
||
[Test]
|
||
public void EachRowStoresAndSavesItsChoice()
|
||
{
|
||
var settings = new AppSettings();
|
||
var saves = 0;
|
||
var rows = new LogSettingsRows(settings, () => saves++);
|
||
var changed = new List<string?>();
|
||
rows.PropertyChanged += (_, e) => changed.Add(e.PropertyName);
|
||
|
||
Assert.SequenceEqual(["Trace", "Debug", "Information", "Warning", "Error"], rows.Levels.Select(l => l.Label));
|
||
Assert.Equal(("Information", "14 days", "50 MB", "1,024 MB", "Daily", "4 MB"), (rows.SelectedLevel.Label, rows.SelectedRetention.Label,
|
||
rows.SelectedFileSize.Label, rows.SelectedTotalSize.Label, rows.SelectedRotation.Label, rows.SelectedDiagnostics.Label));
|
||
Assert.Equal("1 day", rows.RetentionChoices[0].Label);
|
||
// "No limit" closes both size lists.
|
||
Assert.Equal((0, "No limit"), (rows.FileSizeChoices[^1].Value, rows.FileSizeChoices[^1].Label));
|
||
Assert.Equal(0, rows.TotalSizeChoices[^1].Value);
|
||
Assert.SequenceEqual(["Daily", "Hourly"], rows.Rotations.Select(r => r.Label));
|
||
|
||
rows.SelectedLevel = rows.Levels.Single(l => l.Value == LogLevel.Debug);
|
||
rows.SelectedRetention = rows.RetentionChoices.Single(c => c.Value == 90);
|
||
rows.SelectedFileSize = rows.FileSizeChoices[^1];
|
||
rows.SelectedTotalSize = rows.TotalSizeChoices.Single(c => c.Value == 5120);
|
||
rows.SelectedRotation = rows.Rotations.Single(r => r.Value == LogRotation.Hourly);
|
||
rows.SelectedDiagnostics = rows.DiagnosticsChoices.Single(c => c.Value == 16);
|
||
Assert.Equal((LogLevel.Debug, 90, 0, 5120, LogRotation.Hourly, 16),
|
||
(settings.LogLevel, settings.LogRetentionDays, settings.LogMaxFileMb, settings.LogMaxTotalMb, settings.LogRotation, settings.DiagnosticsLogMb));
|
||
Assert.Equal(6, saves);
|
||
Assert.SequenceEqual(["SelectedLevel", "SelectedRetention", "SelectedFileSize", "SelectedTotalSize", "SelectedRotation", "SelectedDiagnostics"], changed);
|
||
|
||
// The same choice again, or none (a list being rebuilt), changes nothing.
|
||
rows.SelectedLevel = rows.Levels.Single(l => l.Value == LogLevel.Debug);
|
||
rows.SelectedRotation = null!;
|
||
Assert.Equal(6, saves);
|
||
|
||
// Values set elsewhere are listed in order with the presets.
|
||
settings.LogRetentionDays = 45;
|
||
settings.LogMaxTotalMb = 3000;
|
||
Assert.SequenceEqual([1, 3, 7, 14, 30, 45, 90, 365], rows.RetentionChoices.Select(c => c.Value));
|
||
Assert.Equal("45 days", rows.SelectedRetention.Label);
|
||
Assert.SequenceEqual([256, 512, 1024, 2048, 3000, 5120, 10240, 0], rows.TotalSizeChoices.Select(c => c.Value));
|
||
|
||
// Reset section on Advanced: the defaults, shown at once; the caller saves.
|
||
changed.Clear();
|
||
rows.Reset();
|
||
Assert.Equal((LogLevel.Info, 14, 50, 1024, LogRotation.Daily, 4),
|
||
(settings.LogLevel, settings.LogRetentionDays, settings.LogMaxFileMb, settings.LogMaxTotalMb, settings.LogRotation, settings.DiagnosticsLogMb));
|
||
Assert.Equal(string.Empty, changed.Single());
|
||
Assert.Equal(6, saves);
|
||
}
|
||
}
|