From 7b2a3c02bb8bfd77976bed0d7c889f6efa788810 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Thu, 14 Dec 2017 11:44:05 +0100
Subject: [PATCH] Use a separate Source for the EventLog.

This allows messages from the updater to be distinguished from ones
coming from the service itself.
---
 Updater/Update-Service.ps1 | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/Updater/Update-Service.ps1 b/Updater/Update-Service.ps1
index d911d2a..c25115a 100644
--- a/Updater/Update-Service.ps1
+++ b/Updater/Update-Service.ps1
@@ -333,22 +333,20 @@ function Send-MailReport([string]$Subject, [string]$Body) {
 function Log-Message([string]$Type, [string]$Message, [int]$Id){
     # NOTE: by convention, this script is setting the $Id parameter to match the
     # numbers for the output types described in 'Help about_Redirection'
-    $msg = "[$($Me)] "
     try {
         Write-EventLog `
             -LogName Application `
-            -Source $ServiceName `
-            -Category 100 `
+            -Source $Me `
+            -Category 0 `
             -EventId $Id `
             -EntryType $Type `
             -Message "[$($Me)]: $($Message)" `
             -ErrorAction Stop
-        $msg += "Logged message (Id=$($Id), Type=$($Type)).`n"
-        $msg += "--- Log Message ---`n$($Message)`n--- Log Message ---`n"
+        $msg = "[EventLog $($Type)/$($Id)]: $($Message)"
     }
     catch {
         $ex = $_.Exception.Message
-        $msg += "Error logging message (Id=$($Id), Type=$($Type))!`n"
+        $msg = "Error logging message (Id=$($Id), Type=$($Type))!`n"
         $msg += "--- Log Message ---`n$($Message)`n--- Log Message ---`n"
         $msg += "--- Exception ---`n$($ex)`n--- Exception ---"
     }
@@ -372,7 +370,6 @@ function Log-Info([string]$Message) {
 
 
 function Log-Debug([string]$Message) {
-    Write-Verbose $Message
     if ($UpdaterDebugLogging) {
         Log-Message -Type Information -Message $Message -Id 5
     }
@@ -391,20 +388,19 @@ catch {
     Exit
 }
 
-if (-Not ([System.Diagnostics.EventLog]::SourceExists($ServiceName))) {
+# NOTE: $MyInvocation is not available when run as ScheduledJob, so we have to
+# set a shortcut for our name explicitly ourselves here:
+$Me = "$($ServiceName)-Updater"
+
+if (-Not ([System.Diagnostics.EventLog]::SourceExists($Me))) {
     try {
-        New-EventLog -LogName Application -Source $ServiceName
+        New-EventLog -LogName Application -Source $Me
     }
     catch {
         $ex = $_.Exception.Message
         Write-Verbose "Error creating event log source: $($ex)"
     }
 }
-
-
-# NOTE: $MyInvocation is not available when run as ScheduledJob, so we have to
-# set a shortcut for our name explicitly ourselves here:
-$Me = "Update-Service"
 Log-Debug "$($Me) started..."
 
 
-- 
GitLab