| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds a PowerShell sink to the logger configuration. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | The Add-KrSinkPowerShell function configures a logging sink that outputs log events to the PowerShell console. I |
| | | 6 | | .PARAMETER LoggerConfig |
| | | 7 | | The Serilog LoggerConfiguration object to which the PowerShell sink will be added. |
| | | 8 | | .PARAMETER RestrictedToMinimumLevel |
| | | 9 | | The minimum log event level required to write to the PowerShell sink. Defaults to Verbose. |
| | | 10 | | .PARAMETER OutputTemplate |
| | | 11 | | The output template string for formatting log messages. Defaults to '{Message:lj}{ErrorRecord}'. |
| | | 12 | | .PARAMETER LevelSwitch |
| | | 13 | | An optional LoggingLevelSwitch to dynamically control the logging level. |
| | | 14 | | .EXAMPLE |
| | | 15 | | Add-KrSinkPowerShell -LoggerConfig $config |
| | | 16 | | |
| | | 17 | | Adds a PowerShell sink to the logging system, allowing log messages to be output to the PowerShell console. |
| | | 18 | | .EXAMPLE |
| | | 19 | | Add-KrSinkPowerShell -LoggerConfig $config -RestrictedToMinimumLevel Information |
| | | 20 | | |
| | | 21 | | Adds a PowerShell sink that only outputs log events at Information level or higher. |
| | | 22 | | .EXAMPLE |
| | | 23 | | Add-KrSinkPowerShell -LoggerConfig $config -OutputTemplate '{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] |
| | | 24 | | Customizes the output template for PowerShell log messages. |
| | | 25 | | .EXAMPLE |
| | | 26 | | Add-KrSinkPowerShell -LoggerConfig $config -LevelSwitch $myLevelSwitch |
| | | 27 | | |
| | | 28 | | Uses a custom LoggingLevelSwitch to control the logging level dynamically. |
| | | 29 | | .NOTES |
| | | 30 | | This function is part of the Kestrun logging infrastructure and should be used to enable PowerShell console logg |
| | | 31 | | #> |
| | | 32 | | function Add-KrSinkPowerShell { |
| | | 33 | | [KestrunRuntimeApi('Everywhere')] |
| | | 34 | | [CmdletBinding()] |
| | | 35 | | [OutputType([Serilog.LoggerConfiguration])] |
| | | 36 | | param( |
| | | 37 | | [Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| | | 38 | | [Serilog.LoggerConfiguration]$LoggerConfig, |
| | | 39 | | |
| | | 40 | | [Parameter(Mandatory = $false)] |
| | | 41 | | [Serilog.Events.LogEventLevel]$RestrictedToMinimumLevel = [Serilog.Events.LogEventLevel]::Verbose, |
| | | 42 | | |
| | | 43 | | [Parameter(Mandatory = $false)] |
| | | 44 | | [string]$OutputTemplate = '{Message:lj}{ErrorRecord}', |
| | | 45 | | |
| | | 46 | | [Parameter(Mandatory = $false)] |
| | | 47 | | [Serilog.Core.LoggingLevelSwitch]$LevelSwitch = $null |
| | | 48 | | ) |
| | | 49 | | |
| | | 50 | | process { |
| | 0 | 51 | | return [Kestrun.Logging.Sinks.Extensions.PowerShellSinkExtensions]::PowerShell($LoggerConfig.WriteTo, |
| | 0 | 52 | | { param([Serilog.Events.LogEvent]$logEvent, [string]$renderedMessage) Write-KrSinkPowerShell -LogEvent $logE |
| | | 53 | | $RestrictedToMinimumLevel, |
| | | 54 | | $OutputTemplate, |
| | | 55 | | $LevelSwitch |
| | | 56 | | ) |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |