< Summary - Kestrun — Combined Coverage

Information
Class: Public.Logging.sinks.Add-KrSinkFile
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Logging/sinks/Add-KrSinkFile.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 9
Coverable lines: 9
Total lines: 159
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 08/26/2025 - 01:25:22 Line coverage: 75% (3/4) Total lines: 147 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a09/04/2025 - 22:37:32 Line coverage: 75% (3/4) Total lines: 148 Tag: Kestrun/Kestrun@afb7aadc0a8a42bfa2b51ea62c8a6e2cf63faec609/06/2025 - 18:30:33 Line coverage: 66.6% (2/3) Total lines: 146 Tag: Kestrun/Kestrun@aeddbedb8a96e9137aac94c2d5edd011b57ac87110/13/2025 - 16:52:37 Line coverage: 0% (0/9) Total lines: 159 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Logging/sinks/Add-KrSinkFile.ps1

#LineLine coverage
 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#>
 51function 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    begin {
 0107        $Path = $Path -replace '[\\/]', [IO.Path]::DirectorySeparatorChar
 0108        $Path = Resolve-KrPath -Path $Path -KestrunRoot
 109        # Ensure directory exists before Serilog attempts to create/append the file
 110        try {
 0111            $directory = [System.IO.Path]::GetDirectoryName($Path)
 0112            if ($directory -and -not [string]::IsNullOrWhiteSpace($directory) -and -not (Test-Path -LiteralPath $directo
 0113                [System.IO.Directory]::CreateDirectory($directory) | Out-Null
 114            }
 115        } catch {
 0116            Write-Warning "Add-KrSinkFile: Failed to ensure log directory '$directory' exists. $_"
 117        }
 118    }
 119    process {
 0120        switch ($PSCmdlet.ParameterSetName) {
 121            'Default' {
 0122                return [Serilog.FileLoggerConfigurationExtensions]::File($LoggerConfig.WriteTo,
 123                    $Path,
 124                    $RestrictedToMinimumLevel,
 125                    $OutputTemplate,
 126                    $FormatProvider,
 127                    $FileSizeLimitBytes,
 128                    $LevelSwitch,
 129                    $Buffered,
 130                    $Shared,
 131                    $FlushToDiskInterval,
 132                    $RollingInterval,
 133                    $RollOnFileSizeLimit,
 134                    $RetainedFileCountLimit,
 135                    $Encoding,
 136                    $Hooks
 137                )
 138            }
 139            'Formatter' {
 0140                return [Serilog.FileLoggerConfigurationExtensions]::File($LoggerConfig.WriteTo,
 141                    $Formatter,
 142                    $Path,
 143                    $RestrictedToMinimumLevel,
 144                    $FileSizeLimitBytes,
 145                    $LevelSwitch,
 146                    $Buffered,
 147                    $Shared,
 148                    $FlushToDiskInterval,
 149                    $RollingInterval,
 150                    $RollOnFileSizeLimit,
 151                    $RetainedFileCountLimit,
 152                    $Encoding,
 153                    $Hooks
 154                )
 155            }
 156        }
 157    }
 158}
 159

Methods/Properties

Add-KrSinkFile()