Skip to content

Commit cae1d6c

Browse files
authoredMar 1, 2025··
Fixes `Logging' init in UI Catalog and naming consistency (#3944)
1 parent a8d3d26 commit cae1d6c

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed
 

‎Terminal.Gui/ConsoleDrivers/V2/Logging.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static void Error (
6767
}
6868

6969
/// <summary>
70-
/// Logs a critical message including the class and method name.
70+
/// Logs a fatal/critical message including the class and method name.
7171
/// </summary>
7272
/// <param name="message"></param>
7373
/// <param name="caller"></param>
@@ -115,7 +115,7 @@ public static void Information (
115115
}
116116

117117
/// <summary>
118-
/// Logs a trace message including the class and method name.
118+
/// Logs a trace/verbose message including the class and method name.
119119
/// </summary>
120120
/// <param name="message"></param>
121121
/// <param name="caller"></param>

‎UICatalog/UICatalog.cs

+25-9
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
using System.Text.Json;
1818
using System.Text.Json.Serialization;
1919
using Microsoft.Extensions.Logging;
20+
using static Terminal.Gui.ConfigurationManager;
21+
using Command = Terminal.Gui.Command;
2022
using Serilog;
2123
using Serilog.Core;
2224
using Serilog.Events;
23-
using Terminal.Gui;
24-
using static Terminal.Gui.ConfigurationManager;
25-
using Command = Terminal.Gui.Command;
2625
using ILogger = Microsoft.Extensions.Logging.ILogger;
2726
using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment;
27+
using Terminal.Gui;
2828

2929
#nullable enable
3030

@@ -171,7 +171,7 @@ private static int Main (string [] args)
171171
// what's the app name?
172172
_logFilePath = $"{LOGFILE_LOCATION}/{Assembly.GetExecutingAssembly ().GetName ().Name}.log";
173173
Option<string> debugLogLevel = new Option<string> ("--debug-log-level", $"The level to use for logging (debug console and {_logFilePath})").FromAmong (
174-
Enum.GetNames<LogEventLevel> ()
174+
Enum.GetNames<LogLevel> ()
175175
);
176176
debugLogLevel.SetDefaultValue("Warning");
177177
debugLogLevel.AddAlias ("-dl");
@@ -234,9 +234,25 @@ private static int Main (string [] args)
234234
return 0;
235235
}
236236

237+
private static LogEventLevel LogLevelToLogEventLevel (LogLevel logLevel)
238+
{
239+
return logLevel switch
240+
{
241+
LogLevel.Trace => LogEventLevel.Verbose,
242+
LogLevel.Debug => LogEventLevel.Debug,
243+
LogLevel.Information => LogEventLevel.Information,
244+
LogLevel.Warning => LogEventLevel.Warning,
245+
LogLevel.Error => LogEventLevel.Error,
246+
LogLevel.Critical => LogEventLevel.Fatal,
247+
LogLevel.None => LogEventLevel.Fatal, // Default to Fatal if None is specified
248+
_ => LogEventLevel.Fatal // Default to Information for any unspecified LogLevel
249+
};
250+
}
251+
237252
private static ILogger CreateLogger ()
238253
{
239254
// Configure Serilog to write logs to a file
255+
_logLevelSwitch.MinimumLevel = LogLevelToLogEventLevel(Enum.Parse<LogLevel> (_options.DebugLogLevel));
240256
Log.Logger = new LoggerConfiguration ()
241257
.MinimumLevel.ControlledBy (_logLevelSwitch)
242258
.Enrich.FromLogContext () // Enables dynamic enrichment
@@ -1295,19 +1311,19 @@ private List<MenuItem []> CreateLoggingMenuItems ()
12951311
[SuppressMessage ("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
12961312
private MenuItem [] CreateLoggingFlagsMenuItems ()
12971313
{
1298-
string [] logLevelMenuStrings = Enum.GetNames<LogEventLevel> ().Select (n => n = "_" + n).ToArray ();
1299-
LogEventLevel [] logLevels = Enum.GetValues<LogEventLevel> ();
1314+
string [] logLevelMenuStrings = Enum.GetNames<LogLevel> ().Select (n => n = "_" + n).ToArray ();
1315+
LogLevel [] logLevels = Enum.GetValues<LogLevel> ();
13001316

13011317
List<MenuItem?> menuItems = new ();
13021318

1303-
foreach (LogEventLevel logLevel in logLevels)
1319+
foreach (LogLevel logLevel in logLevels)
13041320
{
13051321
var item = new MenuItem
13061322
{
13071323
Title = logLevelMenuStrings [(int)logLevel]
13081324
};
13091325
item.CheckType |= MenuItemCheckStyle.Checked;
1310-
item.Checked = Enum.Parse<LogEventLevel> (_options.DebugLogLevel) == logLevel;
1326+
item.Checked = Enum.Parse<LogLevel> (_options.DebugLogLevel) == logLevel;
13111327

13121328
item.Action += () =>
13131329
{
@@ -1319,7 +1335,7 @@ private MenuItem [] CreateLoggingFlagsMenuItems ()
13191335
if (item.Title == logLevelMenuStrings [(int)logLevel] && item.Checked == false)
13201336
{
13211337
_options.DebugLogLevel = Enum.GetName (logLevel)!;
1322-
_logLevelSwitch.MinimumLevel = Enum.Parse<LogEventLevel> (_options.DebugLogLevel);
1338+
_logLevelSwitch.MinimumLevel = LogLevelToLogEventLevel (Enum.Parse<LogLevel> (_options.DebugLogLevel));
13231339
item.Checked = true;
13241340
}
13251341

0 commit comments

Comments
 (0)
Please sign in to comment.