From e5632220d6c7e474b68e0e2001ce100ca0ea3e45 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Wed, 17 Jan 2018 16:06:14 +0100
Subject: [PATCH] Add mail logging targets for "Error" and "Fatal" levels.

Refers to #3
---
 AutoTx/AutoTx.cs | 52 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 50 insertions(+), 2 deletions(-)

diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs
index b686bf2..510548b 100644
--- a/AutoTx/AutoTx.cs
+++ b/AutoTx/AutoTx.cs
@@ -76,7 +76,7 @@ namespace AutoTx
 
         public AutoTx() {
             InitializeComponent();
-            SetupLogging();
+            SetupFileLogging();
             CreateEventLog();
             LoadSettings();
             CreateIncomingDirectories();
@@ -85,7 +85,7 @@ namespace AutoTx
         /// <summary>
         /// Set up NLog logging: targets, rules...
         /// </summary>
-        private void SetupLogging() {
+        private void SetupFileLogging() {
             var logConfig = new LoggingConfiguration();
             var fileTarget = new FileTarget {
                 FileName = ServiceName + ".log",
@@ -102,6 +102,50 @@ namespace AutoTx
             LogManager.Configuration = logConfig;
         }
 
+        /// <summary>
+        /// Configure logging to email targets.
+        /// 
+        /// Depending on the configuration, set up the logging via email. If no SmtpHost or no
+        /// AdminEmailAdress is configured, nothing will be done. If they're set in the config file,
+        /// a log target for messages with level "Fatal" will be configured. In addition, if the
+        /// AdminDebugEmailAdress is set, another target for "Error" level messages is configured
+        /// using this address as recipient.
+        /// </summary>
+        private void SetupMailLogging() {
+            try {
+                if (string.IsNullOrWhiteSpace(_config.SmtpHost) ||
+                    string.IsNullOrWhiteSpace(_config.AdminEmailAdress))
+                    return;
+
+                var logConfig = LogManager.Configuration;
+                var mailTargetFatal = new MailTarget {
+                    SmtpServer = _config.SmtpHost,
+                    SmtpPort = _config.SmtpPort,
+                    To = _config.AdminEmailAdress,
+                    Name = "mailfatal",
+                };
+                logConfig.AddTarget(mailTargetFatal);
+                logConfig.AddRuleForOneLevel(LogLevel.Fatal, mailTargetFatal);
+
+                if (!string.IsNullOrWhiteSpace(_config.AdminDebugEmailAdress)) {
+                    var mailTargetError = new MailTarget {
+                        SmtpServer = _config.SmtpHost,
+                        SmtpPort = _config.SmtpPort,
+                        To = _config.AdminDebugEmailAdress,
+                        Name = "mailerror",
+                    };
+                    logConfig.AddTarget(mailTargetError);
+                    logConfig.AddRuleForOneLevel(LogLevel.Error, mailTargetError);
+                    Log.Info("Configured mail notification for 'Error' messages to {0}", mailTargetError.To);
+                }
+                Log.Info("Configured mail notification for 'Fatal' messages to {0}", mailTargetFatal.To);
+                LogManager.Configuration = logConfig;
+            }
+            catch (Exception ex) {
+                Log.Error("SetupMailLogging(): {0}", ex.Message);
+            }
+        }
+
         /// <summary>
         /// Create the event log if it doesn't exist yet.
         /// </summary>
@@ -198,6 +242,10 @@ namespace AutoTx
         /// Check if loaded configuration is valid, print a summary to the log.
         /// </summary>
         public void CheckConfiguration() {
+            // non-critical / optional configuration parameters:
+            if (!string.IsNullOrWhiteSpace(_config.SmtpHost))
+                SetupMailLogging();
+
             var configInvalid = false;
             if (CheckSpoolingDirectories() == false) {
                 Log.Error("ERROR checking spooling directories (incoming / managed)!");
-- 
GitLab