Public/Log/Set-LogConfiguration.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Function Set-LogConfiguration {
    <#
    .SYNOPSIS
        Sets global logging configuation (used by Write-Log).

    .DESCRIPTION
        It sets $LogConfiguration object which describes logging configuration (see LogConfiguration.ps1).

    .EXAMPLE
        Set-LogConfiguration -LogLevel 'Warn' -LogFile "$PSScriptRoot\test.log"
    #>

    
    [CmdletBinding()]
    [OutputType([void])]
    param(       
        # Logging level threshold. Only messages with this level or above will be logged (default: Info).
        [Parameter(Mandatory=$false)]
        [string] 
        [ValidateSet('', 'Debug', 'Info', 'Warn', 'Error')]
        $LogLevel,
        
        # Path to additional file log or $null if shouldn't log to file (default: $null).
        [Parameter(Mandatory=$false)]
        [string] 
        $LogFile,
        
        # Name of Event Log Source to log to or $null if shouldn't log to Event Log (default: $null).
        [Parameter(Mandatory=$false)]
        [switch] 
        $LogEventLogSource,
        
        # Logging level threshold for Event Log. This would normally have higher threshold than LogLevel (default: Error).
        [Parameter(Mandatory=$false)]
        [switch] 
        [ValidateSet('', 'Debug', 'Info', 'Warn', 'Error')]
        $LogEventLogThreshold
    )

    Begin { 
        if (!(Get-Variable -Scope Script -Name LogConfiguration -ErrorAction SilentlyContinue)) {
            $Script:LogConfiguration = [PSCustomObject]@{
                LogLevel = 'Info';
                LogFile = $null;
                LogEventLogSource = $null;         
                LogEventLogThreshold = 'Error'; 
            }
        }
        if ($LogFile -and (![System.IO.Path]::IsPathRooted($LogFile))) {
            # we need to set absolute path to log file as .NET working directory would be c:\windows\system32
            $LogFile = Join-Path -Path ((Get-Location).ProviderPath) -ChildPath $LogFile
        }
    }
    Process { 
        $newLogConfiguration = $Script:LogConfiguration
        foreach ($param in $PSBoundParameters.GetEnumerator()) {
            if ($param.Key -eq 'LogLevel' -and !($param.Value)) {
                $paramValue = 'Info'
            } 
            elseif ($param.Key -eq 'LogEventLogThreshold' -and !($param.Value)) {
                $paramValue = 'Error'
            } 
            elseif ($param.Key -eq 'Verbose') {
                continue
            }
            else {
                $paramValue = $param.Value
            }
            $newLogConfiguration.$($param.Key) = $paramValue
        }

        if ($PSBoundParameters['Verbose']) {
            $logMsg = "Logging messages with level >= $($newLogConfiguration.LogLevel) to stdout"
            if ($newLogConfiguration.LogFile) {
                $logMsg += " and file '$($newLogConfiguration.LogFile)'"
            }
            if ($newLogConfiguration.LogEventLogSource) {
                $logMsg += ", also logging messages with level >= $($newLogConfiguration.LogEventLogThreshold) to event log source '$($newLogConfiguration.LogEventLogSource)'"
            }
            $logMsg += '.'
            Write-Verbose -Message $logMsg
        }
        $Script:LogConfiguration = $newLogConfiguration
    }

    End {
    }
}