From 79f535ce08062eefa06ea9a0cd85c6ba3a62dc35 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Sat, 20 Feb 2021 23:14:16 +1300 Subject: [PATCH] Fixing broken NLog config --- DisplayMagician/Program.cs | 17 ++++++++++++----- DisplayMagician/ProgramSettings.cs | 13 +++++++------ DisplayMagician/UIForms/SettingsForm.cs | 14 +++++++------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/DisplayMagician/Program.cs b/DisplayMagician/Program.cs index 6a5738e..18ab395 100644 --- a/DisplayMagician/Program.cs +++ b/DisplayMagician/Program.cs @@ -62,6 +62,11 @@ namespace DisplayMagician { DesktopNotificationManagerCompat.RegisterActivator(); // Prepare NLog for logging + + NLog.Common.InternalLogger.LogLevel = NLog.LogLevel.Debug; + NLog.Common.InternalLogger.LogToConsole = true; + NLog.Common.InternalLogger.LogFile = "C:\\Users\\terry\\AppData\\Local\\DisplayMagician\\Logs\\nlog-internal.txt"; + var config = new NLog.Config.LoggingConfiguration(); // Targets where to log to: File and Console @@ -120,19 +125,21 @@ namespace DisplayMagician { var loggingRule = new LoggingRule("LogToFile"); loggingRule.EnableLoggingForLevels(logLevel, NLog.LogLevel.Fatal); loggingRule.Targets.Add(logfile); + loggingRule.LoggerNamePattern = "*"; config.LoggingRules.Add(loggingRule); // Create the log console target - var logconsole = new NLog.Targets.ConsoleTarget() + var logconsole = new NLog.Targets.ColoredConsoleTarget("logconsole") { Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}", }; // Create a logging rule to use the log console target - loggingRule = new LoggingRule("LogToConsole"); - loggingRule.EnableLoggingForLevels(NLog.LogLevel.Info, NLog.LogLevel.Fatal); - loggingRule.Targets.Add(logconsole); - config.LoggingRules.Add(loggingRule); + var loggingRule2 = new LoggingRule("LogToConsole"); + loggingRule2.EnableLoggingForLevels(NLog.LogLevel.Info, NLog.LogLevel.Fatal); + loggingRule2.Targets.Add(logconsole); + loggingRule.LoggerNamePattern = "*"; + config.LoggingRules.Add(loggingRule2); // Apply config NLog.LogManager.Configuration = config; diff --git a/DisplayMagician/ProgramSettings.cs b/DisplayMagician/ProgramSettings.cs index 572e6b5..7a5d508 100644 --- a/DisplayMagician/ProgramSettings.cs +++ b/DisplayMagician/ProgramSettings.cs @@ -108,20 +108,21 @@ namespace DisplayMagician #region Class Methods public static ProgramSettings LoadSettings() { + // NOTE: This function gets called before NLog has setup the logger, meaning + // that we can't log to the log file yet. This is because we need to load the + // loglevel settings so we know what level to configure the logger to write! + // This means we have to only use console.write in this function.... ProgramSettings programSettings = null; - logger.Debug($"ProgramSettings/LoadSettings: Attempting to load ProgramSettings"); - if (File.Exists(_programSettingsStorageJsonFileName)) { string json = ""; try { - logger.Debug($"ProgramSettings/LoadSettings: Found existing ProgramSettings file at {_programSettingsStorageJsonFileName} so loading text from it"); json = File.ReadAllText(_programSettingsStorageJsonFileName, Encoding.Unicode); } catch (Exception ex) { - logger.Error(ex, $"ProgramSettings/LoadSettings: Tried to read the JSON file {_programSettingsStorageJsonFileName} to memory but File.ReadAllTextthrew an exception."); + Console.WriteLine($"ProgramSettings/LoadSettings: Tried to read the JSON file {_programSettingsStorageJsonFileName} to memory from disk but File.ReadAllText threw an exception. {ex}"); } if (!string.IsNullOrWhiteSpace(json)) @@ -138,13 +139,13 @@ namespace DisplayMagician } catch (Exception ex) { - logger.Error(ex,$"ProgramSettings/LoadSettings: Tried to parse the JSON in the {_programSettingsStorageJsonFileName} but the JsonConvert threw an exception."); + Console.WriteLine($"ProgramSettings/LoadSettings: Tried to parse the JSON file {_programSettingsStorageJsonFileName} but the JsonConvert threw an exception. {ex}"); } } } else { - logger.Info($"ProgramSettings/LoadSettings: No ProgramSettings file found. Creating new one at {_programSettingsStorageJsonFileName}"); + Console.WriteLine($"ProgramSettings/LoadSettings: No ProgramSettings file found. Creating new one at {_programSettingsStorageJsonFileName}"); programSettings = new ProgramSettings(); programSettings.SaveSettings(); } diff --git a/DisplayMagician/UIForms/SettingsForm.cs b/DisplayMagician/UIForms/SettingsForm.cs index 410082e..64e3895 100644 --- a/DisplayMagician/UIForms/SettingsForm.cs +++ b/DisplayMagician/UIForms/SettingsForm.cs @@ -146,40 +146,40 @@ namespace DisplayMagician.UIForms if (cmb_loglevel.SelectedItem.Equals(logLevelText["Trace"])) { Program.AppProgramSettings.LogLevel = "Trace"; - config.FindRuleByName("LogToFile").EnableLoggingForLevel(NLog.LogLevel.Trace); + config.FindRuleByName("LogToFile").SetLoggingLevels(NLog.LogLevel.Trace, NLog.LogLevel.Fatal); } else if (cmb_loglevel.SelectedItem.Equals(logLevelText["Debug"])) { Program.AppProgramSettings.LogLevel = "Debug"; - config.FindRuleByName("LogToFile").EnableLoggingForLevel(NLog.LogLevel.Debug); + config.FindRuleByName("LogToFile").SetLoggingLevels(NLog.LogLevel.Debug, NLog.LogLevel.Fatal); } else if (cmb_loglevel.SelectedItem.Equals(logLevelText["Info"])) { Program.AppProgramSettings.LogLevel = "Info"; - config.FindRuleByName("LogToFile").EnableLoggingForLevel(NLog.LogLevel.Info); + config.FindRuleByName("LogToFile").SetLoggingLevels(NLog.LogLevel.Info, NLog.LogLevel.Fatal); } else if (cmb_loglevel.SelectedItem.Equals(logLevelText["Warn"])) { Program.AppProgramSettings.LogLevel = "Warn"; - config.FindRuleByName("LogToFile").EnableLoggingForLevel(NLog.LogLevel.Warn); + config.FindRuleByName("LogToFile").SetLoggingLevels(NLog.LogLevel.Warn, NLog.LogLevel.Fatal); } else if (cmb_loglevel.SelectedItem.Equals(logLevelText["Error"])) { Program.AppProgramSettings.LogLevel = "Error"; - config.FindRuleByName("LogToFile").EnableLoggingForLevel(NLog.LogLevel.Error); + config.FindRuleByName("LogToFile").SetLoggingLevels(NLog.LogLevel.Error, NLog.LogLevel.Fatal); } else if (cmb_loglevel.SelectedItem.Equals(logLevelText["Fatal"])) { Program.AppProgramSettings.LogLevel = "Fatal"; - config.FindRuleByName("LogToFile").EnableLoggingForLevel(NLog.LogLevel.Fatal); + config.FindRuleByName("LogToFile").SetLoggingLevels(NLog.LogLevel.Fatal, NLog.LogLevel.Fatal); } else { Program.AppProgramSettings.LogLevel = "Info"; - config.FindRuleByName("LogToFile").EnableLoggingForLevel(NLog.LogLevel.Info); + config.FindRuleByName("LogToFile").SetLoggingLevels(NLog.LogLevel.Info, NLog.LogLevel.Fatal); } // Use the NLog configuration with the LogLevel we just changed. NLog.LogManager.Configuration = config;