| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Adds environment information to the log context. |
| | 4 | | .DESCRIPTION |
| | 5 | | Adds environment information such as UserName and MachineName to the log context, allowing it to be included in |
| | 6 | | .PARAMETER LoggerConfig |
| | 7 | | Instance of LoggerConfiguration |
| | 8 | | .PARAMETER UserName |
| | 9 | | If specified, enriches logs with the current user's name. |
| | 10 | | .PARAMETER MachineName |
| | 11 | | If specified, enriches logs with the current machine's name. |
| | 12 | | .INPUTS |
| | 13 | | None |
| | 14 | | .OUTPUTS |
| | 15 | | LoggerConfiguration object allowing method chaining |
| | 16 | | .EXAMPLE |
| | 17 | | PS> New-KrLogger | Add-KrEnrichEnvironment | Register-KrLogger |
| | 18 | | #> |
| | 19 | | function Add-KrEnrichEnvironment { |
| | 20 | | [KestrunRuntimeApi('Everywhere')] |
| | 21 | | [OutputType([Serilog.LoggerConfiguration])] |
| | 22 | | [CmdletBinding()] |
| | 23 | | param( |
| | 24 | | [Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| | 25 | | [Serilog.LoggerConfiguration]$loggerConfig, |
| | 26 | | [Parameter(Mandatory = $false)] |
| | 27 | | [switch]$UserName, |
| | 28 | | [Parameter(Mandatory = $false)] |
| | 29 | | [switch]$MachineName |
| | 30 | | ) |
| | 31 | |
|
| | 32 | | process { |
| 0 | 33 | | $hasEnricher = $false |
| | 34 | |
|
| | 35 | | # Only add if UserName is true or both are false (default: both on) |
| 0 | 36 | | if ($UserName -or (-not $UserName.IsPresent -and -not $MachineName.IsPresent)) { |
| 0 | 37 | | $loggerConfig = [Serilog.EnvironmentLoggerConfigurationExtensions]::WithEnvironmentUserName($loggerConfig.En |
| 0 | 38 | | $hasEnricher = $true |
| | 39 | | } |
| | 40 | | # Only add if MachineName is true or both are false (default: both on) |
| 0 | 41 | | if ($MachineName -or (-not $UserName.IsPresent -and -not $MachineName.IsPresent)) { |
| 0 | 42 | | $loggerConfig = [Serilog.EnvironmentLoggerConfigurationExtensions]::WithMachineName($loggerConfig.Enrich) |
| 0 | 43 | | $hasEnricher = $true |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | if (-not $hasEnricher) { |
| 0 | 47 | | Write-Verbose 'No environment enrichers added.' |
| | 48 | | } |
| | 49 | |
|
| 0 | 50 | | return $loggerConfig |
| | 51 | | } |
| | 52 | | } |
| | 53 | |
|