Skip to content
Snippets Groups Projects
Commit e676cc01 authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Make log level adjustable in config file.

Allow setting the log level explicitly from "Warn", "Info", "Debug" or
"Trace" in the configuration.

Default is "Info", fallback for invalid settings is "Debug".

Fixes #34
parent 9798e708
Branches
Tags
No related merge requests found
...@@ -74,9 +74,9 @@ namespace ATxCommon.Serializables ...@@ -74,9 +74,9 @@ namespace ATxCommon.Serializables
#region optional configuration parameters #region optional configuration parameters
/// <summary> /// <summary>
/// Switch on debug log messages. Default: false. /// NLog log level, one of "Warn", "Info", "Debug", "Trace". Default: "Info"
/// </summary> /// </summary>
public bool Debug { get; set; } = false; public string LogLevel { get; set; } = "Info";
/// <summary> /// <summary>
/// Enable debug messages from the RoboSharp library. Default: false. /// Enable debug messages from the RoboSharp library. Default: false.
...@@ -436,6 +436,14 @@ namespace ATxCommon.Serializables ...@@ -436,6 +436,14 @@ namespace ATxCommon.Serializables
if (!c.DestinationDirectory.StartsWith(@"\\")) if (!c.DestinationDirectory.StartsWith(@"\\"))
SubOptimal(c.DestinationDirectory, "DestinationDirectory", "is not a UNC path!"); SubOptimal(c.DestinationDirectory, "DestinationDirectory", "is not a UNC path!");
// LogLevel
var validLogLevels = new List<string> {"Warn", "Info", "Debug", "Trace"};
if (!validLogLevels.Contains(c.LogLevel)) {
SubOptimal(c.LogLevel, "LogLevel", "is invalid, using 'Debug'. Valid options: " +
string.Join(", ", validLogLevels));
c.LogLevel = "Debug";
}
if (string.IsNullOrWhiteSpace(errmsg)) if (string.IsNullOrWhiteSpace(errmsg))
return; return;
...@@ -461,7 +469,7 @@ namespace ATxCommon.Serializables ...@@ -461,7 +469,7 @@ namespace ATxCommon.Serializables
$"MinAvailableMemory: {MinAvailableMemory} MB\n" + $"MinAvailableMemory: {MinAvailableMemory} MB\n" +
"\n" + "\n" +
"############### OPTIONAL PARAMETERS ###############\n" + "############### OPTIONAL PARAMETERS ###############\n" +
$"Debug: {Debug}\n" + $"LogLevel: {LogLevel}\n" +
$"ServiceTimer: {ServiceTimer} ms\n" + $"ServiceTimer: {ServiceTimer} ms\n" +
$"MarkerFile: {MarkerFile}\n" + $"MarkerFile: {MarkerFile}\n" +
$"GracePeriod: {GracePeriod} days (" + $"GracePeriod: {GracePeriod} days (" +
......
...@@ -82,7 +82,7 @@ namespace ATxService ...@@ -82,7 +82,7 @@ namespace ATxService
/// </summary> /// </summary>
public AutoTx() { public AutoTx() {
InitializeComponent(); InitializeComponent();
SetupFileLogging(LogLevel.Debug); SetupFileLogging();
Log.Info("=".PadLeft(80, '=')); Log.Info("=".PadLeft(80, '='));
Log.Info("Attempting to start {0} service...", ServiceName); Log.Info("Attempting to start {0} service...", ServiceName);
CreateEventLog(); CreateEventLog();
...@@ -98,7 +98,22 @@ namespace ATxService ...@@ -98,7 +98,22 @@ namespace ATxService
/// <summary> /// <summary>
/// Set up NLog logging: targets, rules... /// Set up NLog logging: targets, rules...
/// </summary> /// </summary>
private void SetupFileLogging(LogLevel logLevel) { private void SetupFileLogging(string logLevelName = "Debug") {
LogLevel logLevel;
switch (logLevelName) {
case "Warn":
logLevel = LogLevel.Warn;
break;
case "Info":
logLevel = LogLevel.Info;
break;
case "Trace":
logLevel = LogLevel.Trace;
break;
default:
logLevel = LogLevel.Debug;
break;
}
var logConfig = new LoggingConfiguration(); var logConfig = new LoggingConfiguration();
var fileTarget = new FileTarget { var fileTarget = new FileTarget {
Name = "file", Name = "file",
...@@ -234,6 +249,7 @@ namespace ATxService ...@@ -234,6 +249,7 @@ namespace ATxService
// this should terminate the service process: // this should terminate the service process:
throw new Exception("Error loading configuration."); throw new Exception("Error loading configuration.");
} }
SetupFileLogging(_config.LogLevel);
} }
/// <summary> /// <summary>
...@@ -259,9 +275,6 @@ namespace ATxService ...@@ -259,9 +275,6 @@ namespace ATxService
/// Check if loaded configuration is valid, print a summary to the log. /// Check if loaded configuration is valid, print a summary to the log.
/// </summary> /// </summary>
private void CheckConfiguration() { private void CheckConfiguration() {
if (!_config.Debug)
SetupFileLogging(LogLevel.Info);
// non-critical / optional configuration parameters: // non-critical / optional configuration parameters:
if (!string.IsNullOrWhiteSpace(_config.SmtpHost)) if (!string.IsNullOrWhiteSpace(_config.SmtpHost))
SetupMailLogging(); SetupMailLogging();
......
...@@ -30,8 +30,8 @@ ...@@ -30,8 +30,8 @@
<!-- OPTIONAL CONFIGURATION SETTINGS --> <!-- OPTIONAL CONFIGURATION SETTINGS -->
<!-- Debug: enable or disable debug log messages --> <!-- LogLevel: one of "Warn", "Info", "Debug", "Trace" -->
<Debug>true</Debug> <LogLevel>Debug</LogLevel>
<!-- DebugRoboSharp: enable debug messages from the RoboSharp library. --> <!-- DebugRoboSharp: enable debug messages from the RoboSharp library. -->
<DebugRoboSharp>true</DebugRoboSharp> <DebugRoboSharp>true</DebugRoboSharp>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment