From ca1c4d009cacfa4897dc12df15f610f724e0c7a7 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Fri, 2 Mar 2018 22:45:51 +0100
Subject: [PATCH] Add option to request RoboCopy logging to a file.

---
 ATxCommon/Serializables/ServiceConfig.cs |  5 +++++
 ATxService/RoboCommand.cs                | 14 ++++++++++++++
 Resources/conf/config.common.xml         |  3 +++
 3 files changed, 22 insertions(+)

diff --git a/ATxCommon/Serializables/ServiceConfig.cs b/ATxCommon/Serializables/ServiceConfig.cs
index 386ba4a..cdf684e 100644
--- a/ATxCommon/Serializables/ServiceConfig.cs
+++ b/ATxCommon/Serializables/ServiceConfig.cs
@@ -78,6 +78,11 @@ namespace ATxCommon.Serializables
         /// </summary>
         public bool Debug { get; set; } = false;
 
+        /// <summary>
+        /// The full path of a file to be used for RoboCopy log messages. Default: "" (off).
+        /// </summary>
+        public string RoboCopyLog { get; set; } = "";
+
         /// <summary>
         /// The interval (in ms) for checking for new files and system parameters. Default: 1000.
         /// </summary>
diff --git a/ATxService/RoboCommand.cs b/ATxService/RoboCommand.cs
index d3dc6ab..831f375 100644
--- a/ATxService/RoboCommand.cs
+++ b/ATxService/RoboCommand.cs
@@ -80,7 +80,21 @@ namespace ATxService
                 // retry options
                 _roboCommand.RetryOptions.RetryCount = 0;
                 _roboCommand.RetryOptions.RetryWaitTime = 0;
+
+                // robocopy logging (NOTE: the "OutputToRoboSharpAndLog" option is required as
+                // otherwise messages will go to the log only and can't be processed by RoboSharp
+                // any more, resulting in no callbacks and therefore no progress reports etc.)
+                if (!string.IsNullOrWhiteSpace(_config.RoboCopyLog)) {
+                    Log.Debug("Setting RoboCopy log file to [{0}]", _config.RoboCopyLog);
+                    _roboCommand.LoggingOptions = new LoggingOptions {
+                        LogPath = _config.RoboCopyLog,
+                        OutputToRoboSharpAndLog = true,
+                        VerboseOutput = true
+                    };
+                    
+                }
                 _roboCommand.Start();
+
                 Log.Info("Transfer started, total size: {0}",
                     Conv.BytesToString(_status.CurrentTransferSize));
                 Log.Trace("RoboCopy command options: {0}", _roboCommand.CommandOptions);
diff --git a/Resources/conf/config.common.xml b/Resources/conf/config.common.xml
index 8109493..9532985 100644
--- a/Resources/conf/config.common.xml
+++ b/Resources/conf/config.common.xml
@@ -33,6 +33,9 @@
     <!-- Debug: enable or disable debug log messages -->
     <Debug>true</Debug>
 
+    <!-- RoboCopyLog: a file to be used for RoboCopy log messages.-->
+    <RoboCopyLog>C:\Temp\autotx-robocopy.log</RoboCopyLog>
+
     <!-- ServiceTimer: interval (in ms) for checking files and parameters -->
     <ServiceTimer>1000</ServiceTimer>
 
-- 
GitLab