Files
Chronos/src/Chronos.App/Settings/LogSettingsRows.cs
T
Alby96 0e84392f4f 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)
2026-10-27 02:08:09 +01:00

108 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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));
}