From 935c497a7d17cff7127e1511edac3f143c96f0ef Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Wed, 25 Mar 2020 17:50:56 +0100 Subject: [PATCH] Extends eventlog write function for pre-defined messages and object dump --- .../logging/Write-IcingaEventMessage.psm1 | 45 ++++++++++++++++--- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/lib/core/logging/Write-IcingaEventMessage.psm1 b/lib/core/logging/Write-IcingaEventMessage.psm1 index bfb4bb5..904e793 100644 --- a/lib/core/logging/Write-IcingaEventMessage.psm1 +++ b/lib/core/logging/Write-IcingaEventMessage.psm1 @@ -2,15 +2,50 @@ function Write-IcingaEventMessage() { param( [int]$EventId = 0, - [string]$Namespace = $null + [string]$Namespace = $null, + [array]$Objects = @() ); - if ($EventId -eq 0 -Or $null -eq $Namespace) { + if ($EventId -eq 0 -Or [string]::IsNullOrEmpty($Namespace)) { return; } - $EntryType = $IcingaEventLogEnums[$Namespace][$EventId].EntryType; - $Message = $IcingaEventLogEnums[$Namespace][$EventId].Message; + [string]$EntryType = $IcingaEventLogEnums[$Namespace][$EventId].EntryType; + [string]$Message = $IcingaEventLogEnums[$Namespace][$EventId].Message; + [string]$Details = $IcingaEventLogEnums[$Namespace][$EventId].Details; + + if ([string]::IsNullOrEmpty($Details)) { + $Details = ''; + } + if ([string]::IsNullOrEmpty($Message)) { + $Message = ''; + } + + [string]$ObjectDump = ''; + + if ($Objects.Count -eq 0) { + $ObjectDump = [string]::Format( + '{0}{0}No additional object details provided.', + (New-IcingaNewLine) + ); + } + + foreach ($entry in $Objects) { + $ObjectDump = [string]::Format( + '{0}{1}', + $ObjectDump, + ($entry | Out-String) + ); + } + + [string]$EventLogMessage = [string]::Format( + '{0}{1}{1}{2}{1}{1}Object dumps if available:{1}{3}', + $Message, + (New-IcingaNewLine), + $Details, + $ObjectDump + + ); if ($null -eq $EntryType -Or $null -eq $Message) { return; @@ -20,5 +55,5 @@ function Write-IcingaEventMessage() -Source 'Icinga for Windows' ` -EntryType $EntryType ` -EventId $EventId ` - -Message $Message; + -Message $EventLogMessage; }