浏览代码

Fixes `Logging' init in UI Catalog and naming consistency (#3944)

Tig 4 月之前
父节点
当前提交
cae1d6c47e
共有 2 个文件被更改,包括 27 次插入11 次删除
  1. 2 2
      Terminal.Gui/ConsoleDrivers/V2/Logging.cs
  2. 25 9
      UICatalog/UICatalog.cs

+ 2 - 2
Terminal.Gui/ConsoleDrivers/V2/Logging.cs

@@ -67,7 +67,7 @@ public static class Logging
     }
     }
 
 
     /// <summary>
     /// <summary>
-    ///     Logs a critical message including the class and method name.
+    ///     Logs a fatal/critical message including the class and method name.
     /// </summary>
     /// </summary>
     /// <param name="message"></param>
     /// <param name="message"></param>
     /// <param name="caller"></param>
     /// <param name="caller"></param>
@@ -115,7 +115,7 @@ public static class Logging
     }
     }
 
 
     /// <summary>
     /// <summary>
-    ///     Logs a trace message including the class and method name.
+    ///     Logs a trace/verbose message including the class and method name.
     /// </summary>
     /// </summary>
     /// <param name="message"></param>
     /// <param name="message"></param>
     /// <param name="caller"></param>
     /// <param name="caller"></param>

+ 25 - 9
UICatalog/UICatalog.cs

@@ -17,14 +17,14 @@ using System.Text;
 using System.Text.Json;
 using System.Text.Json;
 using System.Text.Json.Serialization;
 using System.Text.Json.Serialization;
 using Microsoft.Extensions.Logging;
 using Microsoft.Extensions.Logging;
+using static Terminal.Gui.ConfigurationManager;
+using Command = Terminal.Gui.Command;
 using Serilog;
 using Serilog;
 using Serilog.Core;
 using Serilog.Core;
 using Serilog.Events;
 using Serilog.Events;
-using Terminal.Gui;
-using static Terminal.Gui.ConfigurationManager;
-using Command = Terminal.Gui.Command;
 using ILogger = Microsoft.Extensions.Logging.ILogger;
 using ILogger = Microsoft.Extensions.Logging.ILogger;
 using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment;
 using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment;
+using Terminal.Gui;
 
 
 #nullable enable
 #nullable enable
 
 
@@ -171,7 +171,7 @@ public class UICatalogApp
         // what's the app name?
         // what's the app name?
         _logFilePath = $"{LOGFILE_LOCATION}/{Assembly.GetExecutingAssembly ().GetName ().Name}.log";
         _logFilePath = $"{LOGFILE_LOCATION}/{Assembly.GetExecutingAssembly ().GetName ().Name}.log";
         Option<string> debugLogLevel = new Option<string> ("--debug-log-level", $"The level to use for logging (debug console and {_logFilePath})").FromAmong (
         Option<string> debugLogLevel = new Option<string> ("--debug-log-level", $"The level to use for logging (debug console and {_logFilePath})").FromAmong (
-             Enum.GetNames<LogEventLevel> ()
+             Enum.GetNames<LogLevel> ()
             );
             );
         debugLogLevel.SetDefaultValue("Warning");
         debugLogLevel.SetDefaultValue("Warning");
         debugLogLevel.AddAlias ("-dl");
         debugLogLevel.AddAlias ("-dl");
@@ -234,9 +234,25 @@ public class UICatalogApp
         return 0;
         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 ()
     private static ILogger CreateLogger ()
     {
     {
         // Configure Serilog to write logs to a file
         // Configure Serilog to write logs to a file
+        _logLevelSwitch.MinimumLevel = LogLevelToLogEventLevel(Enum.Parse<LogLevel> (_options.DebugLogLevel));
         Log.Logger = new LoggerConfiguration ()
         Log.Logger = new LoggerConfiguration ()
                      .MinimumLevel.ControlledBy (_logLevelSwitch)
                      .MinimumLevel.ControlledBy (_logLevelSwitch)
                      .Enrich.FromLogContext () // Enables dynamic enrichment
                      .Enrich.FromLogContext () // Enables dynamic enrichment
@@ -1295,19 +1311,19 @@ public class UICatalogApp
         [SuppressMessage ("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
         [SuppressMessage ("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
         private MenuItem [] CreateLoggingFlagsMenuItems ()
         private MenuItem [] CreateLoggingFlagsMenuItems ()
         {
         {
-            string [] logLevelMenuStrings = Enum.GetNames<LogEventLevel> ().Select (n => n = "_" + n).ToArray ();
-            LogEventLevel [] logLevels = Enum.GetValues<LogEventLevel> ();
+            string [] logLevelMenuStrings = Enum.GetNames<LogLevel> ().Select (n => n = "_" + n).ToArray ();
+            LogLevel [] logLevels = Enum.GetValues<LogLevel> ();
 
 
             List<MenuItem?> menuItems = new ();
             List<MenuItem?> menuItems = new ();
 
 
-            foreach (LogEventLevel logLevel in logLevels)
+            foreach (LogLevel logLevel in logLevels)
             {
             {
                 var item = new MenuItem
                 var item = new MenuItem
                 {
                 {
                     Title = logLevelMenuStrings [(int)logLevel]
                     Title = logLevelMenuStrings [(int)logLevel]
                 };
                 };
                 item.CheckType |= MenuItemCheckStyle.Checked;
                 item.CheckType |= MenuItemCheckStyle.Checked;
-                item.Checked = Enum.Parse<LogEventLevel> (_options.DebugLogLevel) == logLevel;
+                item.Checked = Enum.Parse<LogLevel> (_options.DebugLogLevel) == logLevel;
 
 
                 item.Action += () =>
                 item.Action += () =>
                                {
                                {
@@ -1319,7 +1335,7 @@ public class UICatalogApp
                                    if (item.Title == logLevelMenuStrings [(int)logLevel] && item.Checked == false)
                                    if (item.Title == logLevelMenuStrings [(int)logLevel] && item.Checked == false)
                                    {
                                    {
                                        _options.DebugLogLevel = Enum.GetName (logLevel)!;
                                        _options.DebugLogLevel = Enum.GetName (logLevel)!;
-                                       _logLevelSwitch.MinimumLevel = Enum.Parse<LogEventLevel> (_options.DebugLogLevel);
+                                       _logLevelSwitch.MinimumLevel = LogLevelToLogEventLevel (Enum.Parse<LogLevel> (_options.DebugLogLevel)); 
                                        item.Checked = true;
                                        item.Checked = true;
                                    }
                                    }