| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Adds a file-based logging sink to the logging system. |
| | 4 | | .DESCRIPTION |
| | 5 | | The Add-KrSinkFile function configures a logging sink that writes log events to a specified file. |
| | 6 | | It supports various options for file management, such as rolling intervals, file size limits, and custom output tem |
| | 7 | | .PARAMETER LoggerConfig |
| | 8 | | The Serilog LoggerConfiguration object to which the file sink will be added. |
| | 9 | | .PARAMETER Path |
| | 10 | | The file path where log events will be written. This can include rolling file names. |
| | 11 | | .PARAMETER Formatter |
| | 12 | | An optional text formatter for custom log message formatting. |
| | 13 | | .PARAMETER RestrictedToMinimumLevel |
| | 14 | | The minimum log event level required to write to the file sink. Defaults to Verbose. |
| | 15 | | .PARAMETER OutputTemplate |
| | 16 | | The output template string for formatting log messages. Defaults to '{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Leve |
| | 17 | | .PARAMETER FormatProvider |
| | 18 | | An optional format provider for customizing message formatting. |
| | 19 | | .PARAMETER FileSizeLimitBytes |
| | 20 | | The maximum size of the log file in bytes before it rolls over. Defaults to 1 GB. |
| | 21 | | .PARAMETER LevelSwitch |
| | 22 | | An optional LoggingLevelSwitch to dynamically control the logging level. |
| | 23 | | .PARAMETER Buffered |
| | 24 | | If set, log events are buffered before being written to the file. Defaults to false. |
| | 25 | | .PARAMETER Shared |
| | 26 | | If set, allows multiple processes to write to the same log file. Defaults to false. |
| | 27 | | .PARAMETER FlushToDiskInterval |
| | 28 | | The interval at which the log file is flushed to disk. Defaults to null (no periodic flushing). |
| | 29 | | .PARAMETER RollingInterval |
| | 30 | | The rolling interval for the log file. Defaults to Infinite (no rolling). |
| | 31 | | .PARAMETER RollOnFileSizeLimit |
| | 32 | | If set, the log file will roll over when it reaches the size limit, regardless of the rolling interval. Defaults to |
| | 33 | | .PARAMETER RetainedFileCountLimit |
| | 34 | | The maximum number of rolled log files to retain. Defaults to 31. |
| | 35 | | .PARAMETER Encoding |
| | 36 | | The encoding used for the log file. Defaults to null (system default). |
| | 37 | | .PARAMETER Hooks |
| | 38 | | Lifecycle hooks for managing the log file lifecycle. Defaults to null (no hooks). |
| | 39 | | .EXAMPLE |
| | 40 | | Add-KrSinkFile -LoggerConfig $config -Path "C:\Logs\app-.txt" |
| | 41 | | Adds a file sink to the logging system that writes log events to "C:\Logs\app-.txt". The file name will roll over b |
| | 42 | | .EXAMPLE |
| | 43 | | Add-KrSinkFile -LoggerConfig $config -Path "C:\Logs\app-.txt" -Formatter $formatter |
| | 44 | | Adds a file sink to the logging system that writes log events to "C:\Logs\app-.txt" using the specified text format |
| | 45 | | .EXAMPLE |
| | 46 | | Add-KrSinkFile -LoggerConfig $config -Path "C:\Logs\app-.txt" -RollingInterval Day -RetainedFileCountLimit 7 |
| | 47 | | Adds a file sink that rolls over daily and retains the last 7 log files. |
| | 48 | | .NOTES |
| | 49 | | This function is part of the Kestrun logging infrastructure and should be used to enable file logging. |
| | 50 | | #> |
| | 51 | | function Add-KrSinkFile { |
| | 52 | | [KestrunRuntimeApi('Everywhere')] |
| | 53 | | [CmdletBinding(DefaultParameterSetName = 'Default')] |
| | 54 | | [OutputType([Serilog.LoggerConfiguration])] |
| | 55 | | param( |
| | 56 | | [Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| | 57 | | [Serilog.LoggerConfiguration]$LoggerConfig, |
| | 58 | |
|
| | 59 | | [Parameter(Mandatory = $true)] |
| | 60 | | [string]$Path, |
| | 61 | |
|
| | 62 | | [Parameter(Mandatory = $true, ParameterSetName = 'Formatter')] |
| | 63 | | [Serilog.Formatting.ITextFormatter]$Formatter, |
| | 64 | |
|
| | 65 | | [Parameter(Mandatory = $false)] |
| | 66 | | [Serilog.Events.LogEventLevel]$RestrictedToMinimumLevel = [Serilog.Events.LogEventLevel]::Verbose, |
| | 67 | |
|
| | 68 | | [Parameter(Mandatory = $false)] |
| | 69 | | [string]$OutputTemplate = '{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception} |
| | 70 | | #= '{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{ErrorRecord}{Exception}', |
| | 71 | |
|
| | 72 | | [Parameter(Mandatory = $false, ParameterSetName = 'Default')] |
| | 73 | | [System.IFormatProvider]$FormatProvider = $null, |
| | 74 | |
|
| | 75 | | [Parameter(Mandatory = $false)] |
| | 76 | | [Nullable[long]]$FileSizeLimitBytes = [long]'1073741824', |
| | 77 | |
|
| | 78 | | [Parameter(Mandatory = $false)] |
| | 79 | | [Serilog.Core.LoggingLevelSwitch]$LevelSwitch = $null, |
| | 80 | |
|
| | 81 | | [Parameter(Mandatory = $false)] |
| | 82 | | [switch]$Buffered, |
| | 83 | |
|
| | 84 | | [Parameter(Mandatory = $false)] |
| | 85 | | [switch]$Shared, |
| | 86 | |
|
| | 87 | | [Parameter(Mandatory = $false)] |
| | 88 | | [Nullable[timespan]]$FlushToDiskInterval = $null, |
| | 89 | |
|
| | 90 | | [Parameter(Mandatory = $false)] |
| | 91 | | [Serilog.RollingInterval]$RollingInterval = [Serilog.RollingInterval]::Infinite, |
| | 92 | |
|
| | 93 | | [Parameter(Mandatory = $false)] |
| | 94 | | [switch]$RollOnFileSizeLimit, |
| | 95 | |
|
| | 96 | | [Parameter(Mandatory = $false)] |
| | 97 | | [Nullable[int]]$RetainedFileCountLimit = 31, |
| | 98 | |
|
| | 99 | | [Parameter(Mandatory = $false)] |
| | 100 | | [System.Text.Encoding]$Encoding = $null, |
| | 101 | |
|
| | 102 | | [Parameter(Mandatory = $false)] |
| | 103 | | [Serilog.Sinks.File.FileLifecycleHooks]$Hooks = $null |
| | 104 | | ) |
| | 105 | |
|
| | 106 | | process { |
| 1 | 107 | | switch ($PSCmdlet.ParameterSetName) { |
| | 108 | | 'Default' { |
| 1 | 109 | | return [Serilog.FileLoggerConfigurationExtensions]::File($LoggerConfig.WriteTo, |
| | 110 | | $Path, |
| | 111 | | $RestrictedToMinimumLevel, |
| | 112 | | $OutputTemplate, |
| | 113 | | $FormatProvider, |
| | 114 | | $FileSizeLimitBytes, |
| | 115 | | $LevelSwitch, |
| | 116 | | $Buffered, |
| | 117 | | $Shared, |
| | 118 | | $FlushToDiskInterval, |
| | 119 | | $RollingInterval, |
| | 120 | | $RollOnFileSizeLimit, |
| | 121 | | $RetainedFileCountLimit, |
| | 122 | | $Encoding, |
| | 123 | | $Hooks |
| | 124 | | ) |
| | 125 | | } |
| | 126 | | 'Formatter' { |
| 0 | 127 | | return [Serilog.FileLoggerConfigurationExtensions]::File($LoggerConfig.WriteTo, |
| | 128 | | $Formatter, |
| | 129 | | $Path, |
| | 130 | | $RestrictedToMinimumLevel, |
| | 131 | | $FileSizeLimitBytes, |
| | 132 | | $LevelSwitch, |
| | 133 | | $Buffered, |
| | 134 | | $Shared, |
| | 135 | | $FlushToDiskInterval, |
| | 136 | | $RollingInterval, |
| | 137 | | $RollOnFileSizeLimit, |
| | 138 | | $RetainedFileCountLimit, |
| | 139 | | $Encoding, |
| | 140 | | $Hooks |
| | 141 | | ) |
| | 142 | | } |
| | 143 | | } |
| | 144 | | } |
| | 145 | | } |
| | 146 | |
|