Fixing broken NLog config

This commit is contained in:
Terry MacDonald
2021-02-20 23:14:16 +13:00
parent bb37dc908a
commit 79f535ce08
3 changed files with 26 additions and 18 deletions

View File

@ -62,6 +62,11 @@ namespace DisplayMagician {
DesktopNotificationManagerCompat.RegisterActivator<DesktopNotificationActivator>(); DesktopNotificationManagerCompat.RegisterActivator<DesktopNotificationActivator>();
// Prepare NLog for logging // 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(); var config = new NLog.Config.LoggingConfiguration();
// Targets where to log to: File and Console // Targets where to log to: File and Console
@ -120,19 +125,21 @@ namespace DisplayMagician {
var loggingRule = new LoggingRule("LogToFile"); var loggingRule = new LoggingRule("LogToFile");
loggingRule.EnableLoggingForLevels(logLevel, NLog.LogLevel.Fatal); loggingRule.EnableLoggingForLevels(logLevel, NLog.LogLevel.Fatal);
loggingRule.Targets.Add(logfile); loggingRule.Targets.Add(logfile);
loggingRule.LoggerNamePattern = "*";
config.LoggingRules.Add(loggingRule); config.LoggingRules.Add(loggingRule);
// Create the log console target // 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}", Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}",
}; };
// Create a logging rule to use the log console target // Create a logging rule to use the log console target
loggingRule = new LoggingRule("LogToConsole"); var loggingRule2 = new LoggingRule("LogToConsole");
loggingRule.EnableLoggingForLevels(NLog.LogLevel.Info, NLog.LogLevel.Fatal); loggingRule2.EnableLoggingForLevels(NLog.LogLevel.Info, NLog.LogLevel.Fatal);
loggingRule.Targets.Add(logconsole); loggingRule2.Targets.Add(logconsole);
config.LoggingRules.Add(loggingRule); loggingRule.LoggerNamePattern = "*";
config.LoggingRules.Add(loggingRule2);
// Apply config // Apply config
NLog.LogManager.Configuration = config; NLog.LogManager.Configuration = config;

View File

@ -108,20 +108,21 @@ namespace DisplayMagician
#region Class Methods #region Class Methods
public static ProgramSettings LoadSettings() 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; ProgramSettings programSettings = null;
logger.Debug($"ProgramSettings/LoadSettings: Attempting to load ProgramSettings");
if (File.Exists(_programSettingsStorageJsonFileName)) if (File.Exists(_programSettingsStorageJsonFileName))
{ {
string json = ""; string json = "";
try { try {
logger.Debug($"ProgramSettings/LoadSettings: Found existing ProgramSettings file at {_programSettingsStorageJsonFileName} so loading text from it");
json = File.ReadAllText(_programSettingsStorageJsonFileName, Encoding.Unicode); json = File.ReadAllText(_programSettingsStorageJsonFileName, Encoding.Unicode);
} }
catch (Exception ex) 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)) if (!string.IsNullOrWhiteSpace(json))
@ -138,13 +139,13 @@ namespace DisplayMagician
} }
catch (Exception ex) 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 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 = new ProgramSettings();
programSettings.SaveSettings(); programSettings.SaveSettings();
} }

View File

@ -146,40 +146,40 @@ namespace DisplayMagician.UIForms
if (cmb_loglevel.SelectedItem.Equals(logLevelText["Trace"])) if (cmb_loglevel.SelectedItem.Equals(logLevelText["Trace"]))
{ {
Program.AppProgramSettings.LogLevel = "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"])) else if (cmb_loglevel.SelectedItem.Equals(logLevelText["Debug"]))
{ {
Program.AppProgramSettings.LogLevel = "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"])) else if (cmb_loglevel.SelectedItem.Equals(logLevelText["Info"]))
{ {
Program.AppProgramSettings.LogLevel = "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"])) else if (cmb_loglevel.SelectedItem.Equals(logLevelText["Warn"]))
{ {
Program.AppProgramSettings.LogLevel = "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"])) else if (cmb_loglevel.SelectedItem.Equals(logLevelText["Error"]))
{ {
Program.AppProgramSettings.LogLevel = "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"])) else if (cmb_loglevel.SelectedItem.Equals(logLevelText["Fatal"]))
{ {
Program.AppProgramSettings.LogLevel = "Fatal"; Program.AppProgramSettings.LogLevel = "Fatal";
config.FindRuleByName("LogToFile").EnableLoggingForLevel(NLog.LogLevel.Fatal); config.FindRuleByName("LogToFile").SetLoggingLevels(NLog.LogLevel.Fatal, NLog.LogLevel.Fatal);
} }
else else
{ {
Program.AppProgramSettings.LogLevel = "Info"; 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. // Use the NLog configuration with the LogLevel we just changed.
NLog.LogManager.Configuration = config; NLog.LogManager.Configuration = config;