functions/sinks/Add-SinkEventLog.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
function Add-SinkEventLog {
    <#
    .SYNOPSIS
        Writes log events to console host
    .DESCRIPTION
        Writes log events to console host
    .PARAMETER LoggerConfig
        Instance of LoggerConfiguration
    .PARAMETER Source
        The source name by which the application is registered on the local computer.
    .PARAMETER LogName
        The name of the log the source's entries are written to. Possible values include Application, System, or a custom event log.
    .PARAMETER MachineName
        The name of the machine hosting the event log written to. The local machine by default.
    .PARAMETER ManageEventSource
        If true, check/create event source as required. Defaults to false i.e. do not allow sink to manage event source creation.
    .PARAMETER OutputTemplate
        A message template describing the format used to write to the sink.
    .PARAMETER FormatProvider
        Supplies culture-specific formatting information, or null.
    .PARAMETER RestrictedToMinimumLevel
        The minimum level for events passed through the sink. Ignored when LevelSwitch is specified.
    .PARAMETER EventIdProvider
        Supplies event ids for emitted log events
    .INPUTS
        Instance of LoggerConfiguration
    .OUTPUTS
        LoggerConfiguration object allowing method chaining
    .EXAMPLE
        PS> Add-SinkEventLog -Source 'SampleScript' -ManageEventSource $true
    #>


    [Cmdletbinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Serilog.LoggerConfiguration]$LoggerConfig,
        [Parameter(Mandatory = $true)]
        [string]$Source,
        [Parameter(Mandatory = $false)]
        [string]$LogName,
        [Parameter(Mandatory = $false)]
        [string]$MachineName = '.',
        [Parameter(Mandatory = $false)]
        [string]$ManageEventSource = $false,
        [Parameter(Mandatory = $false)]
        [string]$OutputTemplate = '{Message}{NewLine}{Exception}{ErrorRecord}',
        [Parameter(Mandatory = $false)]
        [System.IFormatProvider]$FormatProvider = $null,
        [Parameter(Mandatory = $false)]
        [Serilog.Events.LogEventLevel]$RestrictedToMinimumLevel = [Serilog.Events.LogEventLevel]::Verbose,
        [Parameter(Mandatory = $false)]
        [Serilog.Sinks.EventLog.IEventIdProvider]$EventIdProvider = $null
    )

    process {    
        $LoggerConfig = [Serilog.LoggerConfigurationEventLogExtensions]::EventLog($LoggerConfig.WriteTo,
            $Source,
            $LogName,
            $MachineName,
            $ManageEventSource,
            $OutputTemplate,
            $FormatProvider,
            $RestrictedToMinimumLevel,
            $EventIdProvider
        )

        $LoggerConfig
    }
}