| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Push a property into Serilog's LogContext for the current scope. |
| | 4 | | .DESCRIPTION |
| | 5 | | Adds a property to Serilog's ambient LogContext so all log events written within the scope |
| | 6 | | include the property. Returns an IDisposable; dispose it to remove the property. |
| | 7 | |
|
| | 8 | | Requires the logger to be configured with Add-KrEnrichFromLogContext. |
| | 9 | | .PARAMETER Name |
| | 10 | | Property name to attach. |
| | 11 | | .PARAMETER Value |
| | 12 | | Property value to attach. |
| | 13 | | .PARAMETER Destructure |
| | 14 | | If set, complex objects will be destructured into structured properties. |
| | 15 | | .OUTPUTS |
| | 16 | | System.IDisposable |
| | 17 | | .EXAMPLE |
| | 18 | | PS> $d = Push-KrLogContextProperty -Name CorrelationId -Value $corr |
| | 19 | | PS> try { Write-KrLog -Level Information -Message 'Hello' } finally { $d.Dispose() } |
| | 20 | | #> |
| | 21 | | function Push-KrLogContextProperty { |
| | 22 | | [KestrunRuntimeApi('Everywhere')] |
| | 23 | | [CmdletBinding()] |
| | 24 | | [OutputType([System.IDisposable])] |
| | 25 | | param( |
| | 26 | | [Parameter(Mandatory = $true)] |
| | 27 | | [string]$Name, |
| | 28 | | [Parameter(Mandatory = $true)] |
| | 29 | | [object]$Value, |
| | 30 | | [Parameter(Mandatory = $false)] |
| | 31 | | [switch]$Destructure |
| | 32 | | ) |
| | 33 | |
|
| | 34 | | process { |
| 0 | 35 | | if ($Destructure) { |
| 0 | 36 | | return [Serilog.Context.LogContext]::PushProperty($Name, $Value, $true) |
| | 37 | | } |
| 0 | 38 | | return [Serilog.Context.LogContext]::PushProperty($Name, $Value) |
| | 39 | | } |
| | 40 | | } |