From dfbac75c959e98780603d586d3f0a2634efae594 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Tue, 13 Mar 2018 18:49:51 +0100
Subject: [PATCH] Move log files to "var/" subdir and include hostname in file
 name

Fixes #5
---
 ATxService/AutoTx.cs       |  4 ++--
 ATxService/Program.cs      |  5 ++++-
 Scripts/Watch-Logfile.ps1  |  2 +-
 Updater/Update-Service.ps1 | 16 ++++++----------
 4 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/ATxService/AutoTx.cs b/ATxService/AutoTx.cs
index 2f95e7f..2d9d8ad 100644
--- a/ATxService/AutoTx.cs
+++ b/ATxService/AutoTx.cs
@@ -157,9 +157,9 @@ namespace ATxService
             var logConfig = new LoggingConfiguration();
             var fileTarget = new FileTarget {
                 Name = "file",
-                FileName = ServiceName + ".log",
+                FileName = $"var/{ServiceName}.{Environment.MachineName}.log",
                 ArchiveAboveSize = 1 * Conv.MegaBytes,
-                ArchiveFileName = ServiceName + ".{#}.log",
+                ArchiveFileName = $"var/{ServiceName}.{Environment.MachineName}" + ".{#}.log",
                 MaxArchiveFiles = 9,
                 KeepFileOpen = true,
                 Layout = LogFormatDefault,
diff --git a/ATxService/Program.cs b/ATxService/Program.cs
index cfab9c9..567c813 100644
--- a/ATxService/Program.cs
+++ b/ATxService/Program.cs
@@ -21,7 +21,10 @@ namespace ATxService
             }
             catch (Exception ex)
             {
-                var startupLog = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "startup.log");
+                var startupLog = Path.Combine(
+                    AppDomain.CurrentDomain.BaseDirectory,
+                    "var",
+                    "startup.log");
                 using (var sw = File.AppendText(startupLog))
                 {
                     sw.WriteLine(ex.Message);
diff --git a/Scripts/Watch-Logfile.ps1 b/Scripts/Watch-Logfile.ps1
index f939e75..3ad6dd3 100644
--- a/Scripts/Watch-Logfile.ps1
+++ b/Scripts/Watch-Logfile.ps1
@@ -6,7 +6,7 @@ if (Test-Path $LocalConfiguration) {
 	Exit
 }
 
-$LogFile = $ServiceDir + "\AutoTx.log"
+$LogFile = "$($ServiceDir)\var\$($ServiceName).$($env:COMPUTERNAME).log"
 
 if (Test-Path $LogFile) {
 	Write-Host "Watching logfile '$LogFile':"
diff --git a/Updater/Update-Service.ps1 b/Updater/Update-Service.ps1
index 3118214..636c6ca 100644
--- a/Updater/Update-Service.ps1
+++ b/Updater/Update-Service.ps1
@@ -159,7 +159,7 @@ function Start-MyService {
         $ex = $_.Exception.Message
         $msg = "Trying to start the service results in this error:`n$($ex)`n`n"
         $msg += " -------------------- last 50 log lines --------------------`n"
-        $msg += Get-LastLogLines "$($LogFile)" 50
+        $msg += Get-LastLogLines "$($LogFilePfx).log" 50
         $msg += " -------------------- ----------------- --------------------`n"
         Send-MailReport -Subject "Startup of service $($ServiceName) failed!" `
             -Body $msg
@@ -450,16 +450,12 @@ function ServiceUpdate-Requested {
 
 
 function Upload-LogFiles {
-    $Dest = "$($UploadPathLogs)\$($env:COMPUTERNAME)"
-    New-Item -Force -Type Directory $Dest | Out-Null
     try {
-        Copy-Item -Force -ErrorAction Stop `
-            -Path "$($LogFile)" `
-            -Destination $Dest
-        Log-Debug "Uploaded logfile to [$($Dest)]."
+        Copy-Item -Force -Path "$($LogFilePfx)*.log" -Destination $UploadPathLogs
+        Log-Debug "Uploaded logfile(s) to [$($UploadPathLogs)]."
     }
     catch {
-        Log-Warning "Uploading logfile FAILED!`n$($_.Exception.Message)"
+        Log-Warning "Uploading logfile(s) FAILED!`n$($_.Exception.Message)"
     }
 }
 
@@ -589,8 +585,8 @@ Log-Debug "$($Me) started..."
 $ServiceRunningBefore = ServiceIsRunning $ServiceName
 
 $ConfigPath = "$($InstallationPath)\conf"
-$LogPath = "$($InstallationPath)"
-$LogFile = "$($LogPath)\AutoTx.log"
+$LogPath = "$($InstallationPath)\var"
+$LogFilePfx = "$($LogPath)\$($ServiceName).$($env:COMPUTERNAME)"
 $StatusXml = "$($InstallationPath)\var\status.xml"
 
 
-- 
GitLab