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

+9
View File
@@ -77,6 +77,7 @@ public partial class App : Application
}
Settings = capture ? new AppSettings { Language = "en" } : AppSettings.Load(Environment.SettingsPath);
ApplyLogSettings();
UiLanguage.Apply(Settings.Language);
ThemeManager.Initialize(this, capture ? AppTheme.Dark : Settings.Theme);
AppResources.Merge(this);
@@ -192,6 +193,14 @@ public partial class App : Application
{
Log.Warning("app", "Settings not saved", ex);
}
// Every settings change passes here, Settings › Advanced › Logs, its Reset and an imported file included.
ApplyLogSettings();
}
/// <summary>Settings › Advanced › Logs to the log files, from the next line on (owner review 2, R11).</summary>
private void ApplyLogSettings()
{
if (_log is not null) AppLogging.Apply(_log, Settings);
}
protected override void OnExit(ExitEventArgs e)
@@ -0,0 +1,38 @@
using Chronos.Core.Diagnostics;
namespace Chronos.App.Infrastructure;
/// <summary>
/// Settings › Advanced › Logs applied to this PC's log files (owner review 2, R11): the level, retention, size limits and
/// rotation of the <see cref="FileLogSink"/> App starts with, and the engine's <see cref="Log.MinimumLevel"/> (the files
/// are the only output outside screenshots). Values outside the ranges Settings offers are brought within them.
/// </summary>
public static class AppLogging
{
public const int DefaultDiagnosticsLogMb = (int)(DiagnosticsArchive.DefaultLogTailBytes / LogFileOptions.BytesPerMb);
public const int MaxRetentionDays = 3650;
public const int MaxFileMb = 10 * 1024;
public const int MaxTotalMb = 1024 * 1024;
public const int MaxDiagnosticsLogMb = 1024;
public static LogFileOptions Options(AppSettings settings) => new()
{
Level = Enum.IsDefined(settings.LogLevel) ? settings.LogLevel : LogLevel.Info,
RetentionDays = int.Clamp(settings.LogRetentionDays, 1, MaxRetentionDays),
MaxFileMb = int.Clamp(settings.LogMaxFileMb, 0, MaxFileMb),
MaxTotalMb = int.Clamp(settings.LogMaxTotalMb, 0, MaxTotalMb),
Rotation = Enum.IsDefined(settings.LogRotation) ? settings.LogRotation : LogRotation.Daily,
};
/// <summary>Bytes the diagnostics bundle keeps from the end of each log file.</summary>
public static long DiagnosticsTailBytes(AppSettings settings) => int.Clamp(settings.DiagnosticsLogMb, 1, MaxDiagnosticsLogMb) * LogFileOptions.BytesPerMb;
/// <summary>The settings to <paramref name="sink"/> and the engine, from the next line on.</summary>
public static void Apply(FileLogSink sink, AppSettings settings)
{
var options = Options(settings);
sink.Configure(options);
Log.MinimumLevel = options.Level;
}
}
@@ -3,6 +3,7 @@ using System.Text.Json;
using System.Text.Json.Serialization;
using Chronos.Core.Analysis.Deep;
using Chronos.Core.Archiving;
using Chronos.Core.Diagnostics;
using Chronos.Core.Import;
using Chronos.Core.IO;
using Chronos.Core.Naming;
@@ -185,6 +186,25 @@ public sealed class AppSettings
/// <summary>"Clear done": finished jobs older than this are hidden from the Jobs list (they stay in the history).</summary>
public DateTimeOffset? JobsClearedAt { get; set; }
// ---- Advanced › Logs (owner review 2, R11; applied by AppLogging) ----
/// <summary>Least severe level written to the log files.</summary>
public LogLevel LogLevel { get; set; } = LogLevel.Info;
/// <summary>Log files last written longer ago are deleted.</summary>
public int LogRetentionDays { get; set; } = LogFileOptions.DefaultRetentionDays;
/// <summary>A log file this large (MB of 2²⁰ bytes) continues in a numbered one; 0 = no limit.</summary>
public int LogMaxFileMb { get; set; } = LogFileOptions.DefaultMaxFileMb;
/// <summary>While the log files together are larger (MB), the oldest are deleted, never the current one; 0 = no limit.</summary>
public int LogMaxTotalMb { get; set; } = LogFileOptions.DefaultMaxTotalMb;
/// <summary>One log file per UTC day or per hour.</summary>
public LogRotation LogRotation { get; set; } = LogRotation.Daily;
/// <summary>MB kept from the end of each log file in the diagnostics bundle.</summary>
public int DiagnosticsLogMb { get; set; } = AppLogging.DefaultDiagnosticsLogMb;
public sealed class WindowPlacement
{
public double Left { get; set; }
@@ -14,7 +14,8 @@ namespace Chronos.App.Infrastructure;
/// <summary>
/// The diagnostics bundle of Settings › Advanced (BRIEF §12.8, §12.9, §22 Phase 8): an environment report, the codec
/// and encoder inventory, the settings, the latest failed jobs and the log files, zipped into a folder the user chose.
/// and encoder inventory, the settings, the latest failed jobs and the end of each log file (Settings › Advanced ›
/// Diagnostics logs), zipped into a folder the user chose.
/// Everything passes through <see cref="SecretScrubber"/> (the saved server token, passwords and keys never leave the
/// PC) and no photo or archive file is ever included.
/// </summary>
@@ -31,6 +32,7 @@ public static class DiagnosticsBundle
public static Task<string> CreateAsync(AppServices services, string folder, IEnumerable<string>? extraLines = null, CancellationToken ct = default)
{
var settings = services.Settings.ToJson();
var tailBytes = AppLogging.DiagnosticsTailBytes(services.Settings);
var report = Report(services.Environment, extraLines) + DatabaseLine(services.Database);
return Task.Run(async () =>
{
@@ -47,7 +49,7 @@ public static class DiagnosticsBundle
bundle.AddText("codecs.txt", codecs);
bundle.AddJson("settings.json", settings);
bundle.AddJobs("jobs-failed.json", failures);
bundle.AddLogs(services.Environment.LogDirectory, cancellationToken: ct);
bundle.AddLogs(services.Environment.LogDirectory, tailBytes: tailBytes, cancellationToken: ct);
}
File.Move(temp, path);
return path;
@@ -617,6 +617,31 @@
<Button Grid.Column="1" Content="{l:Loc Common.Open}" Command="{Binding OpenLogsCommand}" ToolTip="{l:Loc Tooltips.Common.OpenLogs}" Margin="10,0,0,0" />
</Grid>
</HeaderedContentControl>
<!-- Owner review 2, R11: what the log files keep; a choice applies to them at once. -->
<HeaderedContentControl Style="{StaticResource Setting.Row}" Header="{l:Loc Settings.Advanced.LogLevel}">
<ComboBox Width="170" HorizontalAlignment="Left" ItemsSource="{Binding Logs.Levels}" SelectedItem="{Binding Logs.SelectedLevel}" DisplayMemberPath="Label"
ToolTip="{l:Loc Tooltips.Settings.LogLevel}" AutomationProperties.Name="{l:Loc Settings.Advanced.LogLevel}" />
</HeaderedContentControl>
<HeaderedContentControl Style="{StaticResource Setting.Row}" Header="{l:Loc Settings.Advanced.KeepLogs}">
<ComboBox Width="170" HorizontalAlignment="Left" ItemsSource="{Binding Logs.RetentionChoices}" SelectedItem="{Binding Logs.SelectedRetention}"
DisplayMemberPath="Label" ToolTip="{l:Loc Tooltips.Settings.KeepLogs}" AutomationProperties.Name="{l:Loc Settings.Advanced.KeepLogs}" />
</HeaderedContentControl>
<HeaderedContentControl Style="{StaticResource Setting.Row}" Header="{l:Loc Settings.Advanced.FileSize}">
<ComboBox Width="170" HorizontalAlignment="Left" ItemsSource="{Binding Logs.FileSizeChoices}" SelectedItem="{Binding Logs.SelectedFileSize}"
DisplayMemberPath="Label" ToolTip="{l:Loc Tooltips.Settings.LogFileSize}" AutomationProperties.Name="{l:Loc Settings.Advanced.FileSize}" />
</HeaderedContentControl>
<HeaderedContentControl Style="{StaticResource Setting.Row}" Header="{l:Loc Settings.Advanced.LogsTotal}">
<ComboBox Width="170" HorizontalAlignment="Left" ItemsSource="{Binding Logs.TotalSizeChoices}" SelectedItem="{Binding Logs.SelectedTotalSize}"
DisplayMemberPath="Label" ToolTip="{l:Loc Tooltips.Settings.LogsTotal}" AutomationProperties.Name="{l:Loc Settings.Advanced.LogsTotal}" />
</HeaderedContentControl>
<HeaderedContentControl Style="{StaticResource Setting.Row}" Header="{l:Loc Settings.Advanced.Rotation}">
<ComboBox Width="170" HorizontalAlignment="Left" ItemsSource="{Binding Logs.Rotations}" SelectedItem="{Binding Logs.SelectedRotation}" DisplayMemberPath="Label"
ToolTip="{l:Loc Tooltips.Settings.Rotation}" AutomationProperties.Name="{l:Loc Settings.Advanced.Rotation}" />
</HeaderedContentControl>
<HeaderedContentControl Style="{StaticResource Setting.Row}" Header="{l:Loc Settings.Advanced.DiagnosticsLogs}">
<ComboBox Width="170" HorizontalAlignment="Left" ItemsSource="{Binding Logs.DiagnosticsChoices}" SelectedItem="{Binding Logs.SelectedDiagnostics}"
DisplayMemberPath="Label" ToolTip="{l:Loc Tooltips.Settings.DiagnosticsLogs}" AutomationProperties.Name="{l:Loc Settings.Advanced.DiagnosticsLogs}" />
</HeaderedContentControl>
<HeaderedContentControl Style="{StaticResource Setting.Row}" Header="{l:Loc Settings.Advanced.Diagnostics}">
<Button HorizontalAlignment="Left" Content="{l:Loc Settings.Advanced.ExportBundle}" Command="{Binding ExportDiagnosticsCommand}" ToolTip="{l:Loc Tooltips.Settings.Diagnostics}"
ui:ButtonIcon.Glyph="{StaticResource Icon.Export}" />
+21 -7
View File
@@ -30,8 +30,8 @@ namespace Chronos.App.Sections;
/// Archive rows show and change the open archive's own settings (archive.json, through the archive engine) and are the
/// defaults of new archives; Import and Analysis feed the defaults of new cards and the analysis jobs; Rendering feeds
/// the jobs from the next one on; Formats lists the decode tier per format on this PC with the codec extensions to
/// install; Advanced restores the index from a daily backup at the next start; About, the last page, shows this copy
/// (<see cref="ShellViewModel.About"/>, whose update notice Updates shows).
/// install; Advanced sets the log files (<see cref="Logs"/>) and restores the index from a daily backup at the next start;
/// About, the last page, shows this copy (<see cref="ShellViewModel.About"/>, whose update notice Updates shows).
/// </summary>
public sealed class SettingsViewModel : SectionViewModel
{
@@ -83,8 +83,9 @@ public sealed class SettingsViewModel : SectionViewModel
new(SettingsPage.Storage, ["Settings.Storage.Folder", "Settings.Storage.Thumbnails", "Settings.Storage.Limit"]),
new(SettingsPage.Server, ["Settings.Server.Default", "Settings.Server.RenderNode", "Settings.Server.Certificates"]),
new(SettingsPage.Updates, ["Settings.General.CheckOnStart", "Settings.Updates.Latest", "About.Update.Check", "About.Releases"]),
new(SettingsPage.Advanced, ["Settings.Advanced.DataFolder", "Settings.Advanced.Logs", "Settings.Advanced.Diagnostics", "Settings.Advanced.Backup",
"Settings.Advanced.Restore", "Settings.Advanced.Reset"]),
new(SettingsPage.Advanced, ["Settings.Advanced.DataFolder", "Settings.Advanced.Logs", "Settings.Advanced.LogLevel", "Settings.Advanced.KeepLogs",
"Settings.Advanced.FileSize", "Settings.Advanced.LogsTotal", "Settings.Advanced.Rotation", "Settings.Advanced.Diagnostics",
"Settings.Advanced.DiagnosticsLogs", "Settings.Advanced.Backup", "Settings.Advanced.Restore", "Settings.Advanced.Reset"]),
// The translation row shows outside English only, and the language changes while Settings is open.
new(SettingsPage.About, () => ["Shell.AppName", "About.Build", "About.Runtime", "About.System", "About.Licence", "About.ThirdParty",
.. Shell.About.IsTranslated ? (string[])["About.Translation"] : []]),
@@ -156,6 +157,10 @@ public sealed class SettingsViewModel : SectionViewModel
}, () => Loc.L("Tooltips.Settings.Parallelism"));
JobPriorities = [.. PriorityKinds.Select(k => new JobPriorityRow(k, kind => Shell.Services.PriorityQueue.LevelOf(kind), SetJobPriority))];
// ---- Advanced ----
// Saving applies them to the log files (App.SaveSettings).
Logs = new LogSettingsRows(_s, Save);
// ---- Storage ----
CacheFolder = new EditableField(CacheStore.Folder(shell.Services.Environment, _s), v => ArchiveLocations.Validate(v), v =>
{
@@ -765,6 +770,10 @@ public sealed class SettingsViewModel : SectionViewModel
public string DataFolder => Shell.Services.Environment.DataDirectory;
public string LogsFolder => Shell.Services.Environment.LogDirectory;
public string PortableText => Loc.L(Shell.Services.Environment.IsPortable ? "Settings.Advanced.PortableOn" : "Settings.Advanced.PortableOff");
/// <summary>Level, retention, size limits and rotation of the log files, and the diagnostics bundle's share of each.</summary>
public LogSettingsRows Logs { get; }
public ICommand OpenDataFolderCommand { get; }
public ICommand OpenLogsCommand { get; }
public ICommand ExportDiagnosticsCommand { get; }
@@ -807,10 +816,10 @@ public sealed class SettingsViewModel : SectionViewModel
public ICommand ResetSectionCommand { get; }
/// <summary>
/// Advanced holds no setting with a default (paths and actions only), Formats only shows this PC and About only this
/// copy: they have no Reset section.
/// Formats only shows this PC and About only this copy: they have no Reset section. Advanced's resets its log rows
/// (owner review 2, R11); its paths and actions have no default.
/// </summary>
public bool CanResetSection => _page is not (SettingsPage.Advanced or SettingsPage.Formats or SettingsPage.About);
public bool CanResetSection => _page is not (SettingsPage.Formats or SettingsPage.About);
public ICommand ExportSettingsCommand { get; }
public ICommand ImportSettingsCommand { get; }
@@ -1494,6 +1503,9 @@ public sealed class SettingsViewModel : SectionViewModel
case SettingsPage.Updates:
_s.CheckForUpdates = defaults.CheckForUpdates;
break;
case SettingsPage.Advanced:
Logs.Reset();
break;
}
Save();
OnAllPropertiesChanged();
@@ -1599,6 +1611,7 @@ public sealed class SettingsViewModel : SectionViewModel
DefaultServer.Load(ServerAddress.Display(_s.ServerUrl));
_interfacePriority?.Update();
foreach (var row in JobPriorities) row.Refresh();
Logs.Refresh();
UpdatePreview();
LoadCertificates();
OnAllPropertiesChanged();
@@ -1673,6 +1686,7 @@ public sealed class SettingsViewModel : SectionViewModel
foreach (var row in JobPriorities) row.Refresh();
foreach (var row in Formats) row.Refresh();
foreach (var row in MissingExtensions) row.Refresh();
Logs.Refresh();
ArchiveRoot.Refresh();
FolderTemplate.Refresh();
FileTemplate.Refresh();
+107
View File
@@ -0,0 +1,107 @@
using Chronos.App.Infrastructure;
using Chronos.Core.Diagnostics;
using Chronos.Core.Localization;
using Chronos.Ui.Mvvm;
namespace Chronos.App.Settings;
/// <summary>
/// The log rows of Settings › Advanced (owner review 2, R11): level, retention, file size, total size and rotation of this
/// PC's log files, and how much of each the diagnostics bundle keeps. Each choice is stored and saved at once, and saving
/// applies it to the files (<see cref="AppLogging"/>). A value set elsewhere (an imported settings file) is listed with
/// the presets, never shown as another one.
/// </summary>
public sealed class LogSettingsRows(AppSettings settings, Action save) : ObservableObject
{
public static readonly int[] RetentionPresets = [1, 3, 7, 14, 30, 90, 365];
public static readonly int[] FileSizePresets = [10, 20, 50, 100, 200, 500];
public static readonly int[] TotalSizePresets = [256, 512, 1024, 2048, 5120, 10240];
public static readonly int[] DiagnosticsPresets = [1, 2, 4, 8, 16, 32];
public IReadOnlyList<SettingsOption<LogLevel>> Levels =>
[.. Enum.GetValues<LogLevel>().Select(l => new SettingsOption<LogLevel>(l, Loc.L("Settings.Advanced.Level." + l)))];
public SettingsOption<LogLevel> SelectedLevel
{
get => Levels.FirstOrDefault(l => l.Value == settings.LogLevel) ?? Levels.First(l => l.Value == LogLevel.Info);
set => Store(value, settings.LogLevel, v => settings.LogLevel = v);
}
public IReadOnlyList<SettingsOption<int>> RetentionChoices =>
[.. With(RetentionPresets, settings.LogRetentionDays).Select(n => new SettingsOption<int>(n, Loc.Plural("Settings.Advanced.Days", n)))];
public SettingsOption<int> SelectedRetention
{
get => RetentionChoices.First(c => c.Value == settings.LogRetentionDays);
set => Store(value, settings.LogRetentionDays, v => settings.LogRetentionDays = v);
}
public IReadOnlyList<SettingsOption<int>> FileSizeChoices => Sizes(FileSizePresets, settings.LogMaxFileMb);
public SettingsOption<int> SelectedFileSize
{
get => FileSizeChoices.First(c => c.Value == settings.LogMaxFileMb);
set => Store(value, settings.LogMaxFileMb, v => settings.LogMaxFileMb = v);
}
public IReadOnlyList<SettingsOption<int>> TotalSizeChoices => Sizes(TotalSizePresets, settings.LogMaxTotalMb);
public SettingsOption<int> SelectedTotalSize
{
get => TotalSizeChoices.First(c => c.Value == settings.LogMaxTotalMb);
set => Store(value, settings.LogMaxTotalMb, v => settings.LogMaxTotalMb = v);
}
public IReadOnlyList<SettingsOption<LogRotation>> Rotations =>
[.. Enum.GetValues<LogRotation>().Select(r => new SettingsOption<LogRotation>(r, Loc.L("Settings.Advanced.Rotation." + r)))];
public SettingsOption<LogRotation> SelectedRotation
{
get => Rotations.FirstOrDefault(r => r.Value == settings.LogRotation) ?? Rotations[0];
set => Store(value, settings.LogRotation, v => settings.LogRotation = v);
}
public IReadOnlyList<SettingsOption<int>> DiagnosticsChoices =>
[.. With(DiagnosticsPresets, settings.DiagnosticsLogMb).Select(n => new SettingsOption<int>(n, Megabytes(n)))];
public SettingsOption<int> SelectedDiagnostics
{
get => DiagnosticsChoices.First(c => c.Value == settings.DiagnosticsLogMb);
set => Store(value, settings.DiagnosticsLogMb, v => settings.DiagnosticsLogMb = v);
}
/// <summary>The defaults of every row (Reset section on Advanced); the caller saves.</summary>
public void Reset()
{
var defaults = new AppSettings();
settings.LogLevel = defaults.LogLevel;
settings.LogRetentionDays = defaults.LogRetentionDays;
settings.LogMaxFileMb = defaults.LogMaxFileMb;
settings.LogMaxTotalMb = defaults.LogMaxTotalMb;
settings.LogRotation = defaults.LogRotation;
settings.DiagnosticsLogMb = defaults.DiagnosticsLogMb;
Refresh();
}
/// <summary>Labels in the current language, values as the settings have them now.</summary>
internal void Refresh() => OnAllPropertiesChanged();
private void Store<T>(SettingsOption<T>? choice, T current, Action<T> set, [System.Runtime.CompilerServices.CallerMemberName] string? property = null)
{
if (choice is null || EqualityComparer<T>.Default.Equals(choice.Value, current)) return;
set(choice.Value);
save();
OnPropertyChanged(property);
}
/// <summary>The presets in order, then "No limit" (0); <paramref name="current"/> joins them when it is another size.</summary>
private static IReadOnlyList<SettingsOption<int>> Sizes(int[] presets, int current) =>
[
.. With(presets, current).Where(n => n > 0).Select(n => new SettingsOption<int>(n, Megabytes(n))),
new(0, Loc.L("Settings.Advanced.NoLimit")),
];
private static IEnumerable<int> With(int[] presets, int current) => (presets.Contains(current) ? presets : presets.Append(current)).Order();
private static string Megabytes(int n) => Loc.L("Common.Size.MB", Formatting.Count(n));
}