updated the write-loghelper and write-loghybrid

This commit is contained in:
2025-07-02 23:56:13 -04:00
parent 5c37b33bb3
commit 8ed2e06454

View File

@@ -293,48 +293,78 @@
#region Write-Log
# This function is used as a fallback if the SVSMSP module is not installed
# This function is used as a fallback if the SVSMSP module is not installed
function Write-LogHelper {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Message,
[ValidateSet("Info","Warning","Error","Success","General")][string]$Level = "Info",
[ValidateSet("Info","Warning","Error","Success","General")]
[string]$Level = "Info",
[string]$TaskCategory = "GeneralTask",
[switch]$LogToEvent, [string]$EventSource="SVSMSP_Module", [string]$EventLog="Application",
[int]$CustomEventID, [string]$LogFile, [switch]$PassThru
[switch]$LogToEvent,
[string]$EventSource = "Script Automation Monkey",
[string]$EventLog = "SVS Scripting",
[int] $CustomEventID,
[string]$LogFile,
[switch]$PassThru
)
# IDs & colors
# ─── IDs & Colors ────────────────────────────────────────────────
$idMap = @{ Info=1000; Warning=2000; Error=3000; Success=4000; General=1000 }
$colMap = @{ Info="Cyan"; Warning="Yellow"; Error="Red"; Success="Green"; General="White" }
$EventID = if ($PSBoundParameters.CustomEventID) { $CustomEventID } else { $idMap[$Level] }
$color = $colMap[$Level]
$fmt = "[$Level] [$TaskCategory] $Message (Event ID: $EventID)"
# ─── Console Output ─────────────────────────────────────────────
Write-Host $fmt -ForegroundColor $color
# cache
if (-not $Global:LogCache) { $Global:LogCache = @() }
$entry = [pscustomobject]@{ Timestamp=(Get-Date -Format "yyyy-MM-dd HH:mm:ss"); Level=$Level; Message=$fmt }
$Global:LogCache += $entry
# ─── In-Memory Cache ─────────────────────────────────────────────
if (-not $Global:LogCache) { $Global:LogCache = [System.Collections.ArrayList]::new() }
$Global:LogCache.Add([pscustomobject]@{
Timestamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')
Level = $Level
Message = $fmt
}) | Out-Null
# file
# ─── File Logging ────────────────────────────────────────────────
if ($PSBoundParameters.LogFile) {
try { "$($entry.Timestamp) $fmt" | Out-File $LogFile -Append -Encoding UTF8 }
catch { Write-Host "[Warning] File log failed: $_" -ForegroundColor Yellow }
try {
"$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) $fmt" |
Out-File -FilePath $LogFile -Append -Encoding UTF8
}
catch {
Write-Host "[Warning] File log failed: $_" -ForegroundColor Yellow
}
}
# event log
# ─── Event Log ──────────────────────────────────────────────────
if ($LogToEvent) {
$etype = if ($Level -in 'Warning','Error') { $Level } else { 'Information' }
try {
if (-not (Get-EventLog -LogName $EventLog -Source $EventSource -ErrorAction SilentlyContinue)) {
# 1) Ensure your custom source/log exist
if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) {
New-EventLog -LogName $EventLog -Source $EventSource
}
$msg = "TaskCategory:$TaskCategory | Message:$Message"
Write-EventLog -LogName $EventLog -Source $EventSource -EntryType $etype -EventID $EventID -Message $msg
} catch { Write-Host "[Warning] EventLog failed: $_" -ForegroundColor Yellow }
# 2) Map level to entry type
$entryType = if ($Level -in 'Warning','Error') { $Level } else { 'Information' }
# 3) Write to the Windows event log
try {
Write-EventLog `
-LogName $EventLog `
-Source $EventSource `
-EntryType $entryType `
-EventID $EventID `
-Message $fmt
}
catch {
Write-Host "[Warning] EventLog failed: $_" -ForegroundColor Yellow
}
}
if ($PassThru) { return $entry }
# ─── PassThru ────────────────────────────────────────────────────
if ($PassThru) { return $Global:LogCache[-1] }
}
# ─────────────────────────────────────────────────────────────────────────
@@ -349,20 +379,33 @@
[ValidateSet("Info","Warning","Error","Success","General")]
[string]$Level = "Info",
[string]$TaskCategory = "GeneralTask",
[switch]$LogToEvent
[switch]$LogToEvent,
[string]$EventSource = "Script Automation Monkey",
[string]$EventLog = "SVS Scripting"
)
if ( Get-Command -Name Write-Log -ErrorAction SilentlyContinue ) {
# SVSMSP module's Write-Log is available
Write-Log -Message $Message -Level $Level -TaskCategory $TaskCategory -LogToEvent:$LogToEvent
# Real Write-Log; pass through EventSource & EventLog too
Write-Log `
-Message $Message `
-Level $Level `
-TaskCategory $TaskCategory `
-LogToEvent:$LogToEvent `
-EventSource $EventSource `
-EventLog $EventLog
}
else {
# fall back to your helper
Write-LogHelper -Message $Message -Level $Level -TaskCategory $TaskCategory -LogToEvent:$LogToEvent
# Fallback helper: also forward EventSource & EventLog
Write-LogHelper `
-Message $Message `
-Level $Level `
-TaskCategory $TaskCategory `
-LogToEvent:$LogToEvent `
-EventSource $EventSource `
-EventLog $EventLog
}
}
#endregion Write-Log
#region building the Menus