| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Formats a log message for the specified logger and log level. |
| | 4 | | .DESCRIPTION |
| | 5 | | This function takes a log message and its parameters, and formats it for the specified logger and log level. |
| | 6 | | .PARAMETER Logger |
| | 7 | | The Serilog logger instance to use for formatting the message. |
| | 8 | | Pass the logger instance that will be used to format the log message. |
| | 9 | | .PARAMETER Level |
| | 10 | | The log level to use for the log message. |
| | 11 | | Pass the log level that will be used to format the log message. |
| | 12 | | .PARAMETER Message |
| | 13 | | The log message to format. |
| | 14 | | Pass the log message that will be formatted for the specified logger and log level. |
| | 15 | | .PARAMETER Values |
| | 16 | | An array of values to use for formatting the log message. |
| | 17 | | Pass the values that will be used to format the log message. |
| | 18 | | .PARAMETER Exception |
| | 19 | | The exception to include in the log message, if any. |
| | 20 | | Pass the exception that will be included in the log message. |
| | 21 | | .EXAMPLE |
| | 22 | | $formattedMessage = Get-KrFormattedMessage -Logger $logger -Level 'Error' -Message 'An error occurred: {ErrorMes |
| | 23 | | $formattedMessage | Write-Host |
| | 24 | | # Output the formatted message |
| | 25 | | Write-Host $formattedMessage |
| | 26 | | #> |
| | 27 | | function Get-KrFormattedMessage { |
| | 28 | | param( |
| | 29 | | [Parameter(Mandatory = $true)] |
| | 30 | | [Serilog.ILogger]$Logger, |
| | 31 | |
|
| | 32 | | [Parameter(Mandatory = $true)] |
| | 33 | | [Serilog.Events.LogEventLevel]$Level, |
| | 34 | |
|
| | 35 | | [parameter(Mandatory = $true)] |
| | 36 | | [AllowEmptyString()] |
| | 37 | | [string]$Message, |
| | 38 | |
|
| | 39 | | [Parameter(Mandatory = $false)] |
| | 40 | | [AllowNull()] |
| | 41 | | [object[]]$Values, |
| | 42 | |
|
| | 43 | | [Parameter(Mandatory = $false)] |
| | 44 | | [AllowNull()] |
| | 45 | | [System.Exception]$Exception |
| | 46 | | ) |
| | 47 | |
|
| 0 | 48 | | $parsedTemplate = $null |
| 0 | 49 | | $boundProperties = $null |
| 0 | 50 | | if ($Logger.BindMessage($Message, $Values, [ref]$parsedTemplate, [ref]$boundProperties)) { |
| 0 | 51 | | $logEvent = [Serilog.Events.LogEvent]::new([System.DateTimeOffset]::Now, $Level, $Exception, $parsedTemplate, $b |
| 0 | 52 | | $strWriter = [System.IO.StringWriter]::new() |
| | 53 | | # Use the global TextFormatter if available, otherwise use the default formatter from Kestrun.Logging |
| 0 | 54 | | [Kestrun.Logging]::TextFormatter.Format($logEvent, $strWriter) |
| 0 | 55 | | $message = $strWriter.ToString() |
| 0 | 56 | | $strWriter.Dispose() |
| 0 | 57 | | $message |
| | 58 | | } |
| | 59 | | } |
| | 60 | |
|