| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Adds a Syslog UDP sink to the Serilog logger configuration. |
| | 4 | |
|
| | 5 | | .DESCRIPTION |
| | 6 | | Configures a Serilog sink that sends log events to a Syslog server over UDP. |
| | 7 | | Supports hostname, port, app name, format, facility, output template, minimum level, |
| | 8 | | batching, message-id property name (RFC5424), source host, custom severity mapping, |
| | 9 | | custom formatter, dynamic level switch, and structured data ID. |
| | 10 | |
|
| | 11 | | .PARAMETER LoggerConfig |
| | 12 | | The Serilog LoggerConfiguration object to which the Syslog UDP sink will be added. |
| | 13 | |
|
| | 14 | | .PARAMETER Hostname |
| | 15 | | The hostname or IP address of the Syslog server to which log events will be sent. |
| | 16 | |
|
| | 17 | | .PARAMETER Port |
| | 18 | | The port number on which the Syslog server is listening. Defaults to 514. |
| | 19 | |
|
| | 20 | | .PARAMETER AppName |
| | 21 | | The application name to be included in the Syslog messages. If not specified, defaults to the process name. |
| | 22 | |
|
| | 23 | | .PARAMETER Format |
| | 24 | | The Syslog message format to use. Defaults to RFC3164. |
| | 25 | |
|
| | 26 | | .PARAMETER Facility |
| | 27 | | The Syslog facility to use for the log messages. Defaults to Local0. |
| | 28 | |
|
| | 29 | | .PARAMETER OutputTemplate |
| | 30 | | The output template string for formatting log messages (used by the sink’s formatter). |
| | 31 | | If omitted, the sink’s default template/formatter is used. |
| | 32 | |
|
| | 33 | | .PARAMETER RestrictedToMinimumLevel |
| | 34 | | The minimum log event level required to write to the Syslog sink. Defaults to Minimum. |
| | 35 | |
|
| | 36 | | .PARAMETER MessageIdPropertyName |
| | 37 | | For RFC5424 only: property name used to derive the Message ID (default is the sink’s constant). |
| | 38 | |
|
| | 39 | | .PARAMETER SourceHost |
| | 40 | | Optional override for the source host field written by the formatter. |
| | 41 | |
|
| | 42 | | .PARAMETER SeverityMapping |
| | 43 | | Custom delegate to map Serilog levels to syslog severities: |
| | 44 | | [System.Func``2[Serilog.Events.LogEventLevel,Serilog.Sinks.Syslog.Severity]] |
| | 45 | |
|
| | 46 | | .PARAMETER Formatter |
| | 47 | | Optional custom ITextFormatter for full control over message formatting. |
| | 48 | |
|
| | 49 | | .PARAMETER LevelSwitch |
| | 50 | | Optional LoggingLevelSwitch to dynamically control the level. |
| | 51 | |
|
| | 52 | | # ---- Batching (optional; created only if you set any of these) ---- |
| | 53 | | .PARAMETER BatchSizeLimit |
| | 54 | | Maximum number of events per batch. |
| | 55 | |
|
| | 56 | | .PARAMETER PeriodSeconds |
| | 57 | | Flush period in seconds. |
| | 58 | |
|
| | 59 | | .PARAMETER QueueLimit |
| | 60 | | Maximum queued events before dropping. |
| | 61 | |
|
| | 62 | | .PARAMETER EagerlyEmitFirstEvent |
| | 63 | | If specified, the first event is emitted immediately (no waiting for the first period). |
| | 64 | |
|
| | 65 | | .EXAMPLE |
| | 66 | | # simplest: send logs over UDP with defaults |
| | 67 | | Add-KrSinkSyslogUdp -LoggerConfig $config -Hostname "syslog.example.com" |
| | 68 | |
|
| | 69 | | .EXAMPLE |
| | 70 | | # RFC5424 with Local1 facility and custom app name |
| | 71 | | Add-KrSinkSyslogUdp -LoggerConfig $config -Hostname "syslog.example.com" ` |
| | 72 | | -Format RFC5424 -Facility Local1 -AppName "Kestrun" |
| | 73 | |
|
| | 74 | | .EXAMPLE |
| | 75 | | # batching: 50 events, flush every 2s, queue up to 5000, emit first immediately |
| | 76 | | Add-KrSinkSyslogUdp -LoggerConfig $config -Hostname "syslog.example.com" ` |
| | 77 | | -BatchSizeLimit 50 -PeriodSeconds 2 -QueueLimit 5000 -EagerlyEmitFirstEvent |
| | 78 | |
|
| | 79 | | .EXAMPLE |
| | 80 | | # custom severity mapping (Information→Notice, Fatal→Emergency) |
| | 81 | | $map = [System.Func[Serilog.Events.LogEventLevel,Serilog.Sinks.Syslog.Severity]]{ |
| | 82 | | param($level) |
| | 83 | | switch ($level) { |
| | 84 | | 'Information' { [Serilog.Sinks.Syslog.Severity]::Notice } |
| | 85 | | 'Fatal' { [Serilog.Sinks.Syslog.Severity]::Emergency } |
| | 86 | | 'Warning' { [Serilog.Sinks.Syslog.Severity]::Warning } |
| | 87 | | 'Error' { [Serilog.Sinks.Syslog.Severity]::Error } |
| | 88 | | 'Debug' { [Serilog.Sinks.Syslog.Severity]::Debug } |
| | 89 | | 'Verbose' { [Serilog.Sinks.Syslog.Severity]::Debug } |
| | 90 | | default { [Serilog.Sinks.Syslog.Severity]::Informational } |
| | 91 | | } |
| | 92 | | } |
| | 93 | | Add-KrSinkSyslogUdp -LoggerConfig $config -Hostname "syslog.example.com" -SeverityMapping $map |
| | 94 | |
|
| | 95 | | .EXAMPLE |
| | 96 | | # advanced: override message-id property name and source host (RFC5424) |
| | 97 | | Add-KrSinkSyslogUdp -LoggerConfig $config -Hostname "syslog.example.com" ` |
| | 98 | | -Format RFC5424 -MessageIdPropertyName "SourceContext" -SourceHost "api01" |
| | 99 | |
|
| | 100 | | .NOTES |
| | 101 | | This function is part of the Kestrun logging infrastructure and enables Syslog UDP logging. |
| | 102 | | #> |
| | 103 | | function Add-KrSinkSyslogUdp { |
| | 104 | | [KestrunRuntimeApi('Everywhere')] |
| | 105 | | [CmdletBinding()] |
| | 106 | | [OutputType([Serilog.LoggerConfiguration])] |
| | 107 | | param( |
| | 108 | | [Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| | 109 | | [Serilog.LoggerConfiguration]$LoggerConfig, |
| | 110 | |
|
| | 111 | | [Parameter(Mandatory = $true)] |
| | 112 | | [string]$Hostname, |
| | 113 | |
|
| | 114 | | [int]$Port = 514, |
| | 115 | | [string]$AppName = $null, |
| | 116 | |
|
| | 117 | | [Serilog.Sinks.Syslog.SyslogFormat]$Format = [Serilog.Sinks.Syslog.SyslogFormat]::RFC3164, |
| | 118 | | [Serilog.Sinks.Syslog.Facility]$Facility = [Serilog.Sinks.Syslog.Facility]::Local0, |
| | 119 | |
|
| | 120 | | [string]$OutputTemplate = $null, |
| | 121 | | [Serilog.Events.LogEventLevel]$RestrictedToMinimumLevel = [Serilog.Events.LevelAlias]::Minimum, |
| | 122 | |
|
| | 123 | | # extras supported by the sink: |
| | 124 | | [string]$MessageIdPropertyName = [Serilog.Sinks.Syslog.Rfc5424Formatter]::DefaultMessageIdPropertyName, |
| | 125 | | [string]$SourceHost = $null, |
| | 126 | | [System.Func``2[Serilog.Events.LogEventLevel, Serilog.Sinks.Syslog.Severity]]$SeverityMapping = $null, |
| | 127 | | [Serilog.Formatting.ITextFormatter]$Formatter = $null, |
| | 128 | | [Serilog.Core.LoggingLevelSwitch]$LevelSwitch = $null, |
| | 129 | |
|
| | 130 | | # ---- Optional batching knobs ---- |
| | 131 | | [int]$BatchSizeLimit, |
| | 132 | | [int]$PeriodSeconds, |
| | 133 | | [int]$QueueLimit, |
| | 134 | | [switch]$EagerlyEmitFirstEvent |
| | 135 | | ) |
| | 136 | |
|
| | 137 | | process { |
| | 138 | | # Build PeriodicBatchingSinkOptions only if any batching parameter is supplied |
| 0 | 139 | | $batchConfig = $null |
| 0 | 140 | | if ($PSBoundParameters.ContainsKey('BatchSizeLimit') -or |
| | 141 | | $PSBoundParameters.ContainsKey('PeriodSeconds') -or |
| | 142 | | $PSBoundParameters.ContainsKey('QueueLimit') -or |
| | 143 | | $EagerlyEmitFirstEvent.IsPresent) { |
| | 144 | |
|
| 0 | 145 | | $batchConfig = [Serilog.Sinks.PeriodicBatching.PeriodicBatchingSinkOptions]::new() |
| 0 | 146 | | if ($PSBoundParameters.ContainsKey('BatchSizeLimit')) { $batchConfig.BatchSizeLimit = $BatchSizeLimit } |
| 0 | 147 | | if ($PSBoundParameters.ContainsKey('PeriodSeconds')) { $batchConfig.Period = [TimeSpan]::FromSeconds($Period |
| 0 | 148 | | if ($PSBoundParameters.ContainsKey('QueueLimit')) { $batchConfig.QueueLimit = $QueueLimit } |
| 0 | 149 | | if ($EagerlyEmitFirstEvent.IsPresent) { $batchConfig.EagerlyEmitFirstEvent = $true } |
| | 150 | | } |
| | 151 | |
|
| | 152 | | # Call must be strictly positional to match the .NET signature |
| 0 | 153 | | return [Serilog.SyslogLoggerConfigurationExtensions]::UdpSyslog( |
| | 154 | | $LoggerConfig.WriteTo, # 1 loggerSinkConfig (this) |
| | 155 | | $Hostname, # 2 host |
| | 156 | | $Port, # 3 port |
| | 157 | | $AppName, # 4 appName |
| | 158 | | $Format, # 5 format |
| | 159 | | $Facility, # 6 facility |
| | 160 | | $batchConfig, # 7 batchConfig |
| | 161 | | $OutputTemplate, # 8 outputTemplate |
| | 162 | | $RestrictedToMinimumLevel, # 9 restrictedToMinimumLevel |
| | 163 | | $MessageIdPropertyName, # 10 messageIdPropertyName |
| | 164 | | $SourceHost, # 11 sourceHost |
| | 165 | | $SeverityMapping, # 12 severityMapping |
| | 166 | | $Formatter, # 13 formatter |
| | 167 | | $LevelSwitch # 14 levelSwitch |
| | 168 | | ) |
| | 169 | | } |
| | 170 | | } |