Private/Log/Write-LogToEventLog.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function Write-LogToEventLog() {
    <#
    .SYNOPSIS
        Outputs the Message to event log.
    
    .DESCRIPTION
        Creates new event log source if not exists.

    #>


    [CmdletBinding()]
    [OutputType([string])]
    param(
        [string] 
        $Header, 
        
        [string[]] 
        $Message, 
        
        [LogLevel] 
        $Severity
    )
    
    if (!$LogConfiguration.LogEventLogSource -or [int]$Severity -lt [int]$LogConfiguration.LogEventLogThreshold) {
        return
    }
        
    if ($Severity -eq [LogLevel]::ERROR) {
        $entryType = [System.Diagnostics.EventLogEntryType]::Error
    } 
    elseif ($Severity -eq [LogLevel]::WARN) {
        $entryType = [System.Diagnostics.EventLogEntryType]::Warning
    } 
    else {
        $entryType = [System.Diagnostics.EventLogEntryType]::Information
    }

    if (![System.Diagnostics.EventLog]::SourceExists($LogConfiguration.LogEventLogSource)) {
        [void](New-EventLog -LogName Application -Source $LogConfiguration.LogEventLogSource)
    }

    $strBuilder = New-Object System.Text.StringBuilder
    [void]($strBuilder.Append($Header))
    foreach ($msg in $Message) {
        [void]($strBuilder.Append($msg).Append("`r`n"))
    }
    Write-EventLog -LogName Application -Source $LogConfiguration.LogEventLogSource -EntryType $entryType -EventID 1 -Message ($strBuilder.ToString())
}