From e676cc01b401a94a332c868503aa109f6b3a1010 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Wed, 7 Mar 2018 00:09:52 +0100
Subject: [PATCH] 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
---
 ATxCommon/Serializables/ServiceConfig.cs | 14 +++++++++++---
 ATxService/AutoTx.cs                     | 23 ++++++++++++++++++-----
 Resources/conf/config.common.xml         |  4 ++--
 3 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/ATxCommon/Serializables/ServiceConfig.cs b/ATxCommon/Serializables/ServiceConfig.cs
index 2fd864a..378bf1a 100644
--- a/ATxCommon/Serializables/ServiceConfig.cs
+++ b/ATxCommon/Serializables/ServiceConfig.cs
@@ -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 (" +
diff --git a/ATxService/AutoTx.cs b/ATxService/AutoTx.cs
index e8ebfb3..fbb31e9 100644
--- a/ATxService/AutoTx.cs
+++ b/ATxService/AutoTx.cs
@@ -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();
diff --git a/Resources/conf/config.common.xml b/Resources/conf/config.common.xml
index 16e7fa5..b472f53 100644
--- a/Resources/conf/config.common.xml
+++ b/Resources/conf/config.common.xml
@@ -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>
-- 
GitLab