mirror of
https://github.com/terrymacdonald/DisplayMagician.git
synced 2024-08-30 18:32:20 +00:00
Fixing broken NLog config
This commit is contained in:
parent
bb37dc908a
commit
79f535ce08
@ -62,6 +62,11 @@ namespace DisplayMagician {
|
||||
DesktopNotificationManagerCompat.RegisterActivator<DesktopNotificationActivator>();
|
||||
|
||||
// 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;
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user