| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Closes the logger and flushes all logs. |
| | 4 | | .DESCRIPTION |
| | 5 | | Closes the logger and flushes all logs. If no logger is specified, it will close the default logger. |
| | 6 | | .PARAMETER Logger |
| | 7 | | Instance of Serilog.Logger to close. If not specified, the default logger will be closed. |
| | 8 | | .PARAMETER LoggerName |
| | 9 | | Name of the logger to close. If specified, the logger with this name will be closed |
| | 10 | | .PARAMETER DefaultLogger |
| | 11 | | If specified, closes the default logger. |
| | 12 | | .INPUTS |
| | 13 | | Instance of Serilog.Logger |
| | 14 | | .OUTPUTS |
| | 15 | | None. This cmdlet does not return any output. |
| | 16 | | .EXAMPLE |
| | 17 | | PS> Close-KrLogger -Logger $myLogger |
| | 18 | | Closes the specified logger and flushes all logs. |
| | 19 | | .EXAMPLE |
| | 20 | | PS> Close-KrLogger |
| | 21 | | Closes all active loggers and flushes any remaining logs. |
| | 22 | | .EXAMPLE |
| | 23 | | PS> Close-KrLogger -LoggerName 'MyLogger' |
| | 24 | | Closes the logger with the specified name and any remaining logs. |
| | 25 | | .EXAMPLE |
| | 26 | | PS> Close-KrLogger -DefaultLogger |
| | 27 | | Closes the default logger and flushes any remaining logs. |
| | 28 | | #> |
| | 29 | | function Close-KrLogger { |
| | 30 | | [KestrunRuntimeApi('Everywhere')] |
| | 31 | | [CmdletBinding(DefaultParameterSetName = 'AllLogs')] |
| | 32 | | param( |
| | 33 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true, ParameterSetName = 'Logger')] |
| | 34 | | [Serilog.ILogger]$Logger, |
| | 35 | |
|
| | 36 | | [Parameter(Mandatory = $false, ParameterSetName = 'LoggerName')] |
| | 37 | | [string]$LoggerName, |
| | 38 | |
|
| | 39 | | [Parameter(Mandatory = $false, ParameterSetName = 'Default')] |
| | 40 | | [switch]$DefaultLogger |
| | 41 | | ) |
| | 42 | |
|
| | 43 | | process { |
| 0 | 44 | | if ($DefaultLogger) { |
| 0 | 45 | | $Logger = [Kestrun.Logging.LoggerManager]::GetDefault() |
| 0 | 46 | | } elseif ($Null -eq $Logger) { |
| 0 | 47 | | if (-not [string]::IsNullOrEmpty($LoggerName)) { |
| | 48 | | # If LoggerName is specified, get the logger with that name |
| 0 | 49 | | $Logger = [Kestrun.Logging.LoggerManager]::Get($LoggerName) |
| | 50 | | } |
| | 51 | | } |
| 0 | 52 | | if ($null -ne $Logger) { |
| | 53 | | # Close the specified logger |
| 0 | 54 | | $null = [Kestrun.Logging.LoggerManager]::CloseAndFlush($Logger) |
| | 55 | | } else { |
| | 56 | | # Close all loggers |
| 0 | 57 | | [Kestrun.Logging.LoggerManager]::Clear() |
| | 58 | | } |
| | 59 | | } |
| | 60 | | } |
| | 61 | |
|