< Summary - Kestrun — Combined Coverage

Information
Class: Public.Middleware.Add-KrCommonAccessLogMiddleware
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrCommonAccessLogMiddleware.ps1
Tag: Kestrun/Kestrun@6135d944f8787fb570e4dfbacac6e80312799a86
Line coverage
0%
Covered lines: 0
Uncovered lines: 19
Coverable lines: 19
Total lines: 110
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 12/01/2025 - 20:55:19 Line coverage: 0% (0/21) Total lines: 128 Tag: Kestrun/Kestrun@638a27c2dd54103f693f023b6ba5f56a884caafa05/09/2026 - 21:51:36 Line coverage: 0% (0/19) Total lines: 110 Tag: Kestrun/Kestrun@6b24c7512a1bad61723a28d32446de0aa658293e

Coverage delta

Coverage delta 1 -1

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrCommonAccessLogMiddleware.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Adds Apache style common access logging to the Kestrun server.
 4    .DESCRIPTION
 5        Configures the Common Access Log middleware which emits request logs formatted like the
 6        Apache HTTPD common/combined log. The logs are written via the active Serilog pipeline so
 7        any configured sinks receive the access log entries.
 8    .PARAMETER Level
 9        The Serilog log level used when emitting access log entries. Defaults to Information.
 10    .PARAMETER Logger
 11        The Serilog logger instance that should receive the access log entries. When not supplied the
 12        middleware uses the application's default logger from dependency injection.
 13        This parameter is mutually exclusive with LoggerName.
 14    .PARAMETER LoggerName
 15        The name of a registered logger that should receive the access log entries. When supplied
 16        the logger with this name is used instead of the default application logger.
 17        This parameter is mutually exclusive with Logger.
 18    .PARAMETER ExcludeQueryString
 19        Indicates whether the request query string should be excluded from the logged request line. Defaults to $true.
 20    .PARAMETER ExcludeProtocol
 21        Indicates whether the request protocol (for example HTTP/1.1) should be excluded from the logged request line. D
 22    .PARAMETER IncludeElapsedMilliseconds
 23        Appends the total request duration in milliseconds to the access log entry when set to $true. Defaults to $false
 24    .PARAMETER UseUtcTimestamp
 25        When specified the timestamp in the log entry is written in UTC instead of local server time.
 26    .PARAMETER TimestampFormat
 27        Optional custom timestamp format string. When omitted the Apache default "dd/MMM/yyyy:HH:mm:ss zzz" is used.
 28    .PARAMETER ClientAddressHeader
 29        Optional HTTP header name that contains the original client IP (for example X-Forwarded-For).
 30        When supplied the first value from the header is used instead of the socket address.
 31    .EXAMPLE
 32        Add-KrCommonAccessLogMiddleware -LoggerName 'myLogger' -UseUtcTimestamp
 33
 34        Adds the Common Access Log middleware to the current Kestrun server using the named logger 'myLogger'
 35        and configures it to log timestamps in UTC.
 36    .EXAMPLE
 37        New-KrServer -Name "My Server"
 38        Add-KrListener -Port 8080 -IPAddress ([IPAddress]::Any)
 39        Add-KrCommonAccessLogMiddleware -LoggerName 'myLogger'
 40
 41        Creates a new Kestrun server instance, adds a listener on port 8080 and the PowerShell runtime,
 42        then adds the Common Access Log middleware using the named logger 'myLogger' and returns the
 43        server instance in the $server variable.
 44#>
 45function Add-KrCommonAccessLogMiddleware {
 46    [KestrunRuntimeApi('Definition')]
 47    [CmdletBinding(DefaultParameterSetName = 'Logger')]
 48    param(
 49        [Parameter()]
 50        [Serilog.Events.LogEventLevel]$Level = [Serilog.Events.LogEventLevel]::Information,
 51
 52        [Parameter(Mandatory = $false, ParameterSetName = 'Logger')]
 53        [Serilog.ILogger]$Logger,
 54
 55        [Parameter(Mandatory = $true, ParameterSetName = 'LoggerName')]
 56        [string]$LoggerName,
 57
 58        [Parameter()]
 59        [switch]$ExcludeQueryString,
 60
 61        [Parameter()]
 62        [switch]$ExcludeProtocol,
 63
 64        [Parameter()]
 65        [switch]$IncludeElapsedMilliseconds,
 66
 67        [Parameter()]
 68        [switch]$UseUtcTimestamp,
 69
 70        [Parameter()]
 71        [string]$TimestampFormat,
 72
 73        [Parameter()]
 74        [string]$ClientAddressHeader
 75    )
 076    $Server = Resolve-KestrunServer
 77
 78    # If Logger is not provided, use the default logger or the named logger
 079    if ($Null -eq $Logger) {
 080        if ([string]::IsNullOrEmpty($LoggerName)) {
 081            $Logger = [Serilog.Log]::Logger
 82        } else {
 83            # If LoggerName is specified, get the logger with that name
 084            $Logger = [Kestrun.Logging.LoggerManager]::Get($LoggerName)
 85        }
 86    }
 87
 088    $timestampFormatSet = $PSBoundParameters.ContainsKey('TimestampFormat')
 089    $clientHeaderSet = $PSBoundParameters.ContainsKey('ClientAddressHeader')
 90
 091    $options = [Kestrun.Middleware.CommonAccessLogOptions]::new()
 092    $options.Level = $Level
 093    $options.IncludeQueryString = -not $ExcludeQueryString.IsPresent
 094    $options.IncludeProtocol = -not $ExcludeProtocol.IsPresent
 095    $options.IncludeElapsedMilliseconds = $IncludeElapsedMilliseconds.IsPresent
 096    $options.UseUtcTimestamp = $UseUtcTimestamp.IsPresent
 97
 098    if ($timestampFormatSet) {
 099        $options.TimestampFormat = $TimestampFormat
 100    }
 101
 0102    if ($clientHeaderSet -and -not [string]::IsNullOrWhiteSpace($ClientAddressHeader)) {
 0103        $options.ClientAddressHeader = $ClientAddressHeader
 104    }
 105
 0106    $options.Logger = $Logger
 107
 0108    [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddCommonAccessLog($Server, $options) | Out-Null
 109}
 110