functions/Write-InformationLog.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
function Write-InformationLog {
    <#
    .SYNOPSIS
        Writes Information log message
    .DESCRIPTION
        Write a log event with the Information level.
    .PARAMETER MessageTemplate
        Message template describing the event.
    .PARAMETER Logger
        Instance of Serilog.Logger. By default static property [Serilog.Log]::Logger is used.
    .PARAMETER Exception
        Exception related to the event.
    .PARAMETER ErrorRecord
        ErrorRecord related to the event.
    .PARAMETER PropertyValues
        Objects positionally formatted into the message template.
    .PARAMETER PassThru
        Outputs MessageTemplate populated with PropertyValues into pipeline
    .INPUTS
        MessageTemplate - Message template describing the event.
    .OUTPUTS
        None or MessageTemplate populated with PropertyValues into pipeline if PassThru specified
    .EXAMPLE
        PS> Write-InfoLog 'Info log message'
    .EXAMPLE
        PS> Write-InfoLog -MessageTemplate 'Processed {@Position} in {Elapsed:000} ms.' -PropertyValues $position, $elapsedMs
    .EXAMPLE
        PS> Write-InfoLog 'Error occured' -Exception ([System.Exception]::new('Some exception'))
    #>


    [Cmdletbinding(DefaultParameterSetName = 'MsgTemp')]
    param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ParameterSetName = 'MsgTemp')]
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ParameterSetName = 'ErrRec')]
        [AllowEmptyString()]
        [string]$MessageTemplate,

        [Parameter(Mandatory = $false, ParameterSetName = 'MsgTemp')]
        [Parameter(Mandatory = $false, ParameterSetName = 'ErrRec')]
        [Serilog.ILogger]$Logger = [Serilog.Log]::Logger,

        [Parameter(Mandatory = $false, ParameterSetName = 'MsgTemp')]
        [Parameter(Mandatory = $false, ParameterSetName = 'ErrRec')]
        [AllowNull()]
        [System.Exception]$Exception,

        [Parameter(Mandatory = $true, ParameterSetName = 'ErrRec')]
        [Alias("ER")]
        [AllowNull()]
        [System.Management.Automation.ErrorRecord]$ErrorRecord,

        [Parameter(Mandatory = $false, ParameterSetName = 'MsgTemp')]
        [Parameter(Mandatory = $false, ParameterSetName = 'ErrRec')]
        [AllowNull()]
        [object[]]$PropertyValues,

        [Parameter(Mandatory = $false, ParameterSetName = 'MsgTemp')]
        [Parameter(Mandatory = $false, ParameterSetName = 'ErrRec')]
        [switch]$PassThru
    )

    Write-Log -LogLevel Information -MessageTemplate $MessageTemplate -Logger $Logger -Exception $Exception -ErrorRecord $ErrorRecord -PropertyValues $PropertyValues -PassThru:$PassThru
}