| | 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 | | #> |
| | 36 | | function 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 { |
| 0 | 58 | | if ($ManageEventSource.IsPresent) { |
| 0 | 59 | | $ln = if ([string]::IsNullOrWhiteSpace($LogName)) { 'Application' } else { $LogName } |
| | 60 | | try { |
| 0 | 61 | | if (-not [System.Diagnostics.EventLog]::SourceExists($Source)) { |
| 0 | 62 | | [System.Diagnostics.EventLog]::CreateEventSource($Source, $ln) |
| | 63 | | } |
| | 64 | | } catch { |
| 0 | 65 | | throw "Failed to create EventLog source '$Source' in log '$ln'. Run elevated. $_" |
| | 66 | | } |
| | 67 | | } |
| | 68 | |
|
| 0 | 69 | | 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 | | } |