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