| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Adds a Syslog TCP sink to the logging system. |
| | 4 | |
|
| | 5 | | .DESCRIPTION |
| | 6 | | Configures a Serilog sink that sends log events to a Syslog server over TCP. |
| | 7 | | Supports hostname, port, app name, framing, format, facility, TLS, certificate options, |
| | 8 | | output template, minimum level, batching, severity mapping, and advanced syslog parameters. |
| | 9 | |
|
| | 10 | | .PARAMETER LoggerConfig |
| | 11 | | The Serilog LoggerConfiguration object to which the Syslog TCP sink will be added. |
| | 12 | |
|
| | 13 | | .PARAMETER Hostname |
| | 14 | | The hostname or IP address of the Syslog server to which log events will be sent. |
| | 15 | |
|
| | 16 | | .PARAMETER Port |
| | 17 | | The port number on which the Syslog server is listening. Defaults to 514. |
| | 18 | |
|
| | 19 | | .PARAMETER AppName |
| | 20 | | The application name to be included in the Syslog messages. If not specified, defaults to null. |
| | 21 | |
|
| | 22 | | .PARAMETER FramingType |
| | 23 | | The framing type to use for the Syslog messages. Defaults to OCTET_COUNTING. |
| | 24 | |
|
| | 25 | | .PARAMETER Format |
| | 26 | | The Syslog message format to use. Defaults to RFC5424. |
| | 27 | |
|
| | 28 | | .PARAMETER Facility |
| | 29 | | The Syslog facility to use for the log messages. Defaults to Local0. |
| | 30 | |
|
| | 31 | | .PARAMETER UseTls |
| | 32 | | Switch to enable TLS encryption for the TCP connection. Defaults to false. |
| | 33 | |
|
| | 34 | | .PARAMETER CertProvider |
| | 35 | | An optional certificate provider for secure connections. |
| | 36 | |
|
| | 37 | | .PARAMETER CertValidationCallback |
| | 38 | | An optional callback for validating server certificates. |
| | 39 | |
|
| | 40 | | .PARAMETER OutputTemplate |
| | 41 | | The output template string for formatting log messages. |
| | 42 | |
|
| | 43 | | .PARAMETER RestrictedToMinimumLevel |
| | 44 | | The minimum log event level required to write to the Syslog sink. Defaults to Verbose. |
| | 45 | |
|
| | 46 | | .PARAMETER MessageIdPropertyName |
| | 47 | | The property name used for RFC5424 message ID. Defaults to the sink’s built-in constant. |
| | 48 | |
|
| | 49 | | .PARAMETER BatchSizeLimit |
| | 50 | | Maximum number of events per batch (optional). |
| | 51 | |
|
| | 52 | | .PARAMETER PeriodSeconds |
| | 53 | | Flush period for batches in seconds (optional). |
| | 54 | |
|
| | 55 | | .PARAMETER QueueLimit |
| | 56 | | Maximum number of buffered events (optional). |
| | 57 | |
|
| | 58 | | .PARAMETER EagerlyEmitFirstEvent |
| | 59 | | If specified, the first event is sent immediately without waiting for the batch period. |
| | 60 | |
|
| | 61 | | .PARAMETER SourceHost |
| | 62 | | Optional value for the `sourceHost` field in syslog messages. |
| | 63 | |
|
| | 64 | | .PARAMETER SeverityMapping |
| | 65 | | Custom delegate to map Serilog log levels to syslog severities. |
| | 66 | |
|
| | 67 | | .PARAMETER Formatter |
| | 68 | | Optional custom ITextFormatter for full control over message formatting. |
| | 69 | |
|
| | 70 | | .PARAMETER LevelSwitch |
| | 71 | | Optional LoggingLevelSwitch to dynamically control the log level. |
| | 72 | |
|
| | 73 | | .EXAMPLE |
| | 74 | | # simplest: send logs over tcp with defaults |
| | 75 | | Add-KrSinkSyslogTcp -LoggerConfig $config -Hostname "syslog.example.com" |
| | 76 | |
|
| | 77 | | .EXAMPLE |
| | 78 | | # custom port, app name, and TLS enabled |
| | 79 | | Add-KrSinkSyslogTcp -LoggerConfig $config -Hostname "syslog.example.com" -Port 6514 -AppName "MyApp" -UseTls |
| | 80 | |
|
| | 81 | | .EXAMPLE |
| | 82 | | # use RFC3164 format and Local1 facility |
| | 83 | | Add-KrSinkSyslogTcp -LoggerConfig $config -Hostname "syslog.example.com" -Format RFC3164 -Facility Local1 |
| | 84 | |
|
| | 85 | | .EXAMPLE |
| | 86 | | # add batching configuration |
| | 87 | | Add-KrSinkSyslogTcp -LoggerConfig $config -Hostname "syslog.example.com" ` |
| | 88 | | -BatchSizeLimit 50 -PeriodSeconds 1 -QueueLimit 5000 -EagerlyEmitFirstEvent |
| | 89 | |
|
| | 90 | | .EXAMPLE |
| | 91 | | # apply a custom severity mapping |
| | 92 | | $map = [System.Func[Serilog.Events.LogEventLevel,Serilog.Sinks.Syslog.Severity]]{ |
| | 93 | | param($level) |
| | 94 | | switch ($level) { |
| | 95 | | 'Information' { [Serilog.Sinks.Syslog.Severity]::Notice } |
| | 96 | | 'Fatal' { [Serilog.Sinks.Syslog.Severity]::Emergency } |
| | 97 | | default { [Serilog.Sinks.Syslog.Severity]::Informational } |
| | 98 | | } |
| | 99 | | } |
| | 100 | | Add-KrSinkSyslogTcp -LoggerConfig $config -Hostname "syslog.example.com" -SeverityMapping $map |
| | 101 | |
|
| | 102 | | .EXAMPLE |
| | 103 | | # advanced: secure connection with certificate validation |
| | 104 | | $callback = [System.Net.Security.RemoteCertificateValidationCallback]{ |
| | 105 | | param($sender, $cert, $chain, $errors) $true |
| | 106 | | } |
| | 107 | | Add-KrSinkSyslogTcp -LoggerConfig $config -Hostname "syslog.example.com" -UseTls ` |
| | 108 | | -CertValidationCallback $callback -AppName "SecureApp" |
| | 109 | |
|
| | 110 | | .NOTES |
| | 111 | | This function is part of the Kestrun logging infrastructure and should be used to enable Syslog TCP logging. |
| | 112 | | #> |
| | 113 | | function Add-KrSinkSyslogTcp { |
| | 114 | | [KestrunRuntimeApi('Everywhere')] |
| | 115 | | [CmdletBinding()] |
| | 116 | | [OutputType([Serilog.LoggerConfiguration])] |
| | 117 | | param( |
| | 118 | | [Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| | 119 | | [Serilog.LoggerConfiguration]$LoggerConfig, |
| | 120 | |
|
| | 121 | | [Parameter(Mandatory = $true)] |
| | 122 | | [string]$Hostname, |
| | 123 | |
|
| | 124 | | [int]$Port = 514, |
| | 125 | | [string]$AppName = $null, |
| | 126 | |
|
| | 127 | | [Serilog.Sinks.Syslog.FramingType]$FramingType = [Serilog.Sinks.Syslog.FramingType]::OCTET_COUNTING, |
| | 128 | | [Serilog.Sinks.Syslog.SyslogFormat]$Format = [Serilog.Sinks.Syslog.SyslogFormat]::RFC5424, |
| | 129 | | [Serilog.Sinks.Syslog.Facility]$Facility = [Serilog.Sinks.Syslog.Facility]::Local0, |
| | 130 | |
|
| | 131 | | [switch]$UseTls, |
| | 132 | |
|
| | 133 | | [Serilog.Sinks.Syslog.ICertificateProvider]$CertProvider = $null, |
| | 134 | | [System.Net.Security.RemoteCertificateValidationCallback]$CertValidationCallback = $null, |
| | 135 | |
|
| | 136 | | [string]$OutputTemplate, |
| | 137 | | [Serilog.Events.LogEventLevel]$RestrictedToMinimumLevel = [Serilog.Events.LogEventLevel]::Verbose, |
| | 138 | |
|
| | 139 | | [string]$MessageIdPropertyName = [Serilog.Sinks.Syslog.Rfc5424Formatter]::DefaultMessageIdPropertyName, |
| | 140 | |
|
| | 141 | | [int]$BatchSizeLimit, |
| | 142 | | [int]$PeriodSeconds, |
| | 143 | | [int]$QueueLimit, |
| | 144 | | [switch]$EagerlyEmitFirstEvent, |
| | 145 | |
|
| | 146 | | [string]$SourceHost = $null, |
| | 147 | | [System.Func``2[Serilog.Events.LogEventLevel, Serilog.Sinks.Syslog.Severity]]$SeverityMapping = $null, |
| | 148 | | [Serilog.Formatting.ITextFormatter]$Formatter = $null, |
| | 149 | | [Serilog.Core.LoggingLevelSwitch]$LevelSwitch = $null |
| | 150 | | ) |
| | 151 | |
|
| | 152 | | process { |
| | 153 | | # Build PeriodicBatchingSinkOptions if batching args provided |
| 0 | 154 | | $batchConfig = $null |
| 0 | 155 | | if ($PSBoundParameters.ContainsKey('BatchSizeLimit') -or |
| | 156 | | $PSBoundParameters.ContainsKey('PeriodSeconds') -or |
| | 157 | | $PSBoundParameters.ContainsKey('QueueLimit') -or |
| | 158 | | $EagerlyEmitFirstEvent.IsPresent) { |
| | 159 | |
|
| 0 | 160 | | $batchConfig = [Serilog.Sinks.PeriodicBatching.PeriodicBatchingSinkOptions]::new() |
| 0 | 161 | | if ($PSBoundParameters.ContainsKey('BatchSizeLimit')) { $batchConfig.BatchSizeLimit = $BatchSizeLimit } |
| 0 | 162 | | if ($PSBoundParameters.ContainsKey('PeriodSeconds')) { $batchConfig.Period = [TimeSpan]::FromSeconds($Period |
| 0 | 163 | | if ($PSBoundParameters.ContainsKey('QueueLimit')) { $batchConfig.QueueLimit = $QueueLimit } |
| 0 | 164 | | if ($EagerlyEmitFirstEvent.IsPresent) { $batchConfig.EagerlyEmitFirstEvent = $true } |
| | 165 | | } |
| | 166 | |
|
| 0 | 167 | | return [Serilog.SyslogLoggerConfigurationExtensions]::TcpSyslog( |
| | 168 | | $LoggerConfig.WriteTo, # 1 loggerSinkConfig |
| | 169 | | $Hostname, # 2 host |
| | 170 | | $Port, # 3 port |
| | 171 | | $AppName, # 4 appName |
| | 172 | | $FramingType, # 5 framingType |
| | 173 | | $Format, # 6 format |
| | 174 | | $Facility, # 7 facility |
| | 175 | | $UseTls.IsPresent, # 8 useTls (bool) |
| | 176 | | $CertProvider, # 9 certProvider |
| | 177 | | $CertValidationCallback, # 10 certValidationCallback |
| | 178 | | $OutputTemplate, # 11 outputTemplate |
| | 179 | | $RestrictedToMinimumLevel, # 12 restrictedToMinimumLevel |
| | 180 | | $MessageIdPropertyName, # 13 messageIdPropertyName |
| | 181 | | $batchConfig, # 14 batchConfig |
| | 182 | | $SourceHost, # 15 sourceHost |
| | 183 | | $SeverityMapping, # 16 severityMapping |
| | 184 | | $Formatter, # 17 formatter |
| | 185 | | $LevelSwitch # 18 levelSwitch |
| | 186 | | ) |
| | 187 | | } |
| | 188 | | } |