diff --git a/Terminal.Gui/ConsoleDrivers/V2/Logging.cs b/Terminal.Gui/ConsoleDrivers/V2/Logging.cs index 3257e8bd5..8cf6f9a7a 100644 --- a/Terminal.Gui/ConsoleDrivers/V2/Logging.cs +++ b/Terminal.Gui/ConsoleDrivers/V2/Logging.cs @@ -67,7 +67,7 @@ public static class Logging } /// - /// Logs a critical message including the class and method name. + /// Logs a fatal/critical message including the class and method name. /// /// /// @@ -115,7 +115,7 @@ public static class Logging } /// - /// Logs a trace message including the class and method name. + /// Logs a trace/verbose message including the class and method name. /// /// /// diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index bbcafcd5d..f0c13ad12 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -17,14 +17,14 @@ using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using Microsoft.Extensions.Logging; +using static Terminal.Gui.ConfigurationManager; +using Command = Terminal.Gui.Command; using Serilog; using Serilog.Core; using Serilog.Events; -using Terminal.Gui; -using static Terminal.Gui.ConfigurationManager; -using Command = Terminal.Gui.Command; using ILogger = Microsoft.Extensions.Logging.ILogger; using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment; +using Terminal.Gui; #nullable enable @@ -171,7 +171,7 @@ public class UICatalogApp // what's the app name? _logFilePath = $"{LOGFILE_LOCATION}/{Assembly.GetExecutingAssembly ().GetName ().Name}.log"; Option debugLogLevel = new Option ("--debug-log-level", $"The level to use for logging (debug console and {_logFilePath})").FromAmong ( - Enum.GetNames () + Enum.GetNames () ); debugLogLevel.SetDefaultValue("Warning"); debugLogLevel.AddAlias ("-dl"); @@ -234,9 +234,25 @@ public class UICatalogApp return 0; } + private static LogEventLevel LogLevelToLogEventLevel (LogLevel logLevel) + { + return logLevel switch + { + LogLevel.Trace => LogEventLevel.Verbose, + LogLevel.Debug => LogEventLevel.Debug, + LogLevel.Information => LogEventLevel.Information, + LogLevel.Warning => LogEventLevel.Warning, + LogLevel.Error => LogEventLevel.Error, + LogLevel.Critical => LogEventLevel.Fatal, + LogLevel.None => LogEventLevel.Fatal, // Default to Fatal if None is specified + _ => LogEventLevel.Fatal // Default to Information for any unspecified LogLevel + }; + } + private static ILogger CreateLogger () { // Configure Serilog to write logs to a file + _logLevelSwitch.MinimumLevel = LogLevelToLogEventLevel(Enum.Parse (_options.DebugLogLevel)); Log.Logger = new LoggerConfiguration () .MinimumLevel.ControlledBy (_logLevelSwitch) .Enrich.FromLogContext () // Enables dynamic enrichment @@ -1295,19 +1311,19 @@ public class UICatalogApp [SuppressMessage ("Style", "IDE1006:Naming Styles", Justification = "")] private MenuItem [] CreateLoggingFlagsMenuItems () { - string [] logLevelMenuStrings = Enum.GetNames ().Select (n => n = "_" + n).ToArray (); - LogEventLevel [] logLevels = Enum.GetValues (); + string [] logLevelMenuStrings = Enum.GetNames ().Select (n => n = "_" + n).ToArray (); + LogLevel [] logLevels = Enum.GetValues (); List menuItems = new (); - foreach (LogEventLevel logLevel in logLevels) + foreach (LogLevel logLevel in logLevels) { var item = new MenuItem { Title = logLevelMenuStrings [(int)logLevel] }; item.CheckType |= MenuItemCheckStyle.Checked; - item.Checked = Enum.Parse (_options.DebugLogLevel) == logLevel; + item.Checked = Enum.Parse (_options.DebugLogLevel) == logLevel; item.Action += () => { @@ -1319,7 +1335,7 @@ public class UICatalogApp if (item.Title == logLevelMenuStrings [(int)logLevel] && item.Checked == false) { _options.DebugLogLevel = Enum.GetName (logLevel)!; - _logLevelSwitch.MinimumLevel = Enum.Parse (_options.DebugLogLevel); + _logLevelSwitch.MinimumLevel = LogLevelToLogEventLevel (Enum.Parse (_options.DebugLogLevel)); item.Checked = true; }