Private/Logging/Write-PWSHYBKPIVFileLogEntry.ps1

function Write-PWSHYBKPIVFileLogEntry {
    <#
    .SYNOPSIS
        Appends one formatted log line to the configured log file.
    .DESCRIPTION
        Resolves the log file path from the Logging.File configuration and appends a single
        line. Never throws: a failure (bad path, permissions, locked file) is surfaced once
        per session via Write-Warning and silently skipped afterwards, so a broken logging
        destination never interrupts the calling cmdlet's actual yubico-piv-tool.exe work.
    .PARAMETER File
        The Logging.File section (Path, FileName) as returned by Get-PWSHYBKPIVLoggingConfig.
    .PARAMETER Entry
        The log entry object (Timestamp, Level, UserName, CmdletName, Message) as built by
        Write-PWSHYBKPIVLog.
    .EXAMPLE
        Write-PWSHYBKPIVFileLogEntry -File $loggingConfig.File -Entry $entry
        Appends $entry to the resolved log file.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [PSCustomObject] $File,

        [Parameter(Mandatory)]
        [PSCustomObject] $Entry
    )

    try {
        $logPath = Resolve-PWSHYBKPIVLogFilePath -File $File

        $line = '{0:yyyy-MM-dd HH:mm:ss.fff} [{1}] [{2}] [{3}] {4}' -f `
            $Entry.Timestamp, $Entry.Level.ToUpper(), $Entry.UserName, $Entry.CmdletName, $Entry.Message

        Add-Content -Path $logPath -Value $line -Encoding UTF8
    } catch {
        if (-not $script:LoggingSinkFailed) {
            $script:LoggingSinkFailed = $true
            Write-Warning "Posh-YBKPIV file logging failed and will be skipped for the rest of this session: $_"
        }
    }
}