< Summary - Kestrun — Combined Coverage

Information
Class: Public.Logging.sinks.Add-KrSinkEventLog
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Logging/sinks/Add-KrSinkEventLog.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
0%
Covered lines: 0
Uncovered lines: 6
Coverable lines: 6
Total lines: 81
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Logging/sinks/Add-KrSinkEventLog.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Adds an Event Log sink to the Serilog logger configuration.
 4    .DESCRIPTION
 5        The Add-KrSinkEventLog function configures a logging sink that writes log events to the Windows Event Log. It al
 6    .PARAMETER LoggerConfig
 7        The Serilog LoggerConfiguration object to which the Event Log sink will be added.
 8    .PARAMETER Source
 9        The source name for the Event Log. This is used to identify the application or service that is writing to the lo
 10    .PARAMETER LogName
 11        The name of the Event Log to write to. If not specified, defaults to 'Application'.
 12    .PARAMETER MachineName
 13        The name of the machine hosting the Event Log. The local machine by default.
 14    .PARAMETER ManageEventSource
 15        If set to true, the function will attempt to create the event source if it does not exist. Defaults to false.
 16    .PARAMETER OutputTemplate
 17        The output template string for formatting log messages. Defaults to '{Message}{NewLine}{Exception}{ErrorRecord}'
 18    .PARAMETER FormatProvider
 19        An optional format provider for customizing message formatting.
 20    .PARAMETER RestrictedToMinimumLevel
 21        The minimum log event level required to write to the Event Log sink. Defaults to Verbose.
 22    .PARAMETER EventIdProvider
 23        An optional IEventIdProvider to provide custom event IDs for log events.
 24    .EXAMPLE
 25        Add-KrSinkEventLog -LoggerConfig $config -Source "MyApp" -LogName "Application"
 26        Adds an Event Log sink to the logging system that writes log events to the 'Application' log with the source 'My
 27    .EXAMPLE
 28        Add-KrSinkEventLog -LoggerConfig $config -Source "MyApp" -LogName "CustomLog"
 29        Adds an Event Log sink to the logging system that writes log events to the 'CustomLog' log with the source 'MyAp
 30    .EXAMPLE
 31        Add-KrSinkEventLog -LoggerConfig $config -Source "MyApp" -LogName "Application" -ManageEventSource $true
 32        Adds an Event Log sink that manages the event source, creating it if it does not exist.
 33    .NOTES
 34        This function is part of the Kestrun logging infrastructure and should be used to enable Event Log logging.
 35    #>
 36function Add-KrSinkEventLog {
 37    [KestrunRuntimeApi('Everywhere')]
 38    [CmdletBinding()]
 39    [OutputType([Serilog.LoggerConfiguration])]
 40    param(
 41        [Parameter(Mandatory, ValueFromPipeline)]
 42        [Serilog.LoggerConfiguration]$LoggerConfig,
 43
 44        [Parameter(Mandatory = $true)]
 45        [string]$Source,
 46
 47        [string]$LogName = $null,                         # null → sink defaults to 'Application'
 48        [string]$MachineName = '.',
 49        [switch]$ManageEventSource,
 50
 51        [string]$OutputTemplate = '{Message}{NewLine}{Exception}{ErrorRecord}',
 52        [System.IFormatProvider]$FormatProvider = $null,
 53        [Serilog.Events.LogEventLevel]$RestrictedToMinimumLevel = [Serilog.Events.LogEventLevel]::Verbose,
 54        [Serilog.Sinks.EventLog.IEventIdProvider]$EventIdProvider = $null
 55    )
 56
 57    process {
 058        if ($ManageEventSource.IsPresent) {
 059            $ln = if ([string]::IsNullOrWhiteSpace($LogName)) { 'Application' } else { $LogName }
 60            try {
 061                if (-not [System.Diagnostics.EventLog]::SourceExists($Source)) {
 062                    [System.Diagnostics.EventLog]::CreateEventSource($Source, $ln)
 63                }
 64            } catch {
 065                throw "Failed to create EventLog source '$Source' in log '$ln'. Run elevated. $_"
 66            }
 67        }
 68
 069        return [Serilog.LoggerConfigurationEventLogExtensions]::EventLog(
 70            $LoggerConfig.WriteTo,
 71            $Source,
 72            $LogName,
 73            $MachineName,
 74            $ManageEventSource.IsPresent,
 75            $OutputTemplate,
 76            $FormatProvider,
 77            $RestrictedToMinimumLevel,
 78            $EventIdProvider
 79        )
 80    }
 81}

Methods/Properties

Add-KrSinkEventLog()