Multiple Loggers & Levels

Run more than one named logger at once and control their verbosity separately. In this sample we use:

  • app logger: Minimum Information, Console + File logs/app<timestamp>.log.
  • audit logger: Minimum Debug, File logs/audit<timestamp>.log.

Tip: If you need deeper concepts, see the Logging guide.

Full source

File: pwsh/tutorial/examples/5.2-Multiple-Loggers-Levels.ps1

<#
    Sample demonstrating multiple named loggers with different minimum levels.
    'app' logger: Minimum Information, writes to console + app.log
    'audit' logger: Minimum Debug, writes to audit.log
    FileName: 5.2-Multiple-Loggers-Levels.ps1
#>

param(
    [int]$Port = 5000,
    [IPAddress]$IPAddress = [IPAddress]::Loopback
)

$appLogger = New-KrLogger |
    Set-KrLoggerLevel -Value Information |
    Add-KrSinkConsole |
    Add-KrSinkFile -Path '.\logs\app.log' -RollingInterval Hour |
    Register-KrLogger -Name 'app' -PassThru

$auditLogger = New-KrLogger |
    Set-KrLoggerLevel -Value Debug |
    Add-KrSinkFile -Path '.\logs\audit.log' -RollingInterval Hour |
    Register-KrLogger -Name 'audit' -PassThru

# Create server and listener
New-KrServer -Name 'Multiple Loggers Server'
Add-KrEndpoint -Port $Port -IPAddress $IPAddress

# PowerShell runtime is required for script block routes


Write-KrLog -LoggerName 'app' -Level Information -Message 'Server created'
Write-KrLog -LoggerName 'audit' -Level Debug -Message 'Audit logger active'

Enable-KrConfiguration

# Routes
Add-KrMapRoute -Verbs Get -Path '/info' -ScriptBlock {
    Write-KrLog -LoggerName 'app' -Level Information -Message 'Info route handled'
    Write-KrTextResponse -InputObject 'info' -StatusCode 200
}

Add-KrMapRoute -Verbs Get -Path '/debug' -ScriptBlock {
    # This Debug will be filtered out by 'app' (min Information)
    Write-KrLog -LoggerName 'app' -Level Debug -Message 'App debug (filtered)'
    # This Debug will be written by 'audit' (min Debug)
    Write-KrLog -LoggerName 'audit' -Level Debug -Message 'Audit debug (written)'
    Write-KrTextResponse -InputObject 'debug' -StatusCode 200
}

Add-KrMapRoute -Verbs Get -Path '/audit' -ScriptBlock {
    Write-KrLog -LoggerName 'audit' -Level Information -Message 'Audit event recorded'
    Write-KrTextResponse -InputObject 'audit' -StatusCode 200
}



# Start the server
Start-KrServer

# Clean up and close all the loggers when the server stops
Close-KrLogger


Step-by-step

  1. Create the ‘app’ logger with minimum Information, Console + File sinks, then register it as app.
  2. Create the ‘audit’ logger with minimum Debug, File sink only, register as audit.
  3. Standard server + listener + PowerShell runtime.
  4. Apply configuration with Enable-KrConfiguration to commit the listener/runtime.
  5. Routes:
    • /info writes Information to app.
    • /debug writes Debug to both loggers; app filters Debug (not written), audit writes it.
    • /audit demonstrates an Information event on audit.
  6. Start server with Start-KrServer.

Try it

Save the sample locally so the log files are easy to find. Copy the contents of pwsh/tutorial/examples/5.2-Multiple-Loggers-Levels.ps1 into a new file in an empty working folder (for example, multi-loggers.ps1), then run:

# From your working folder
pwsh .\multi-loggers.ps1
curl http://127.0.0.1:5000/info
curl http://127.0.0.1:5000/debug
curl http://127.0.0.1:5000/audit
Get-Content .\logs\app*.log -Tail 20
Get-Content .\logs\audit*.log -Tail 20

Expected: Debug lines appear only in audit<timestamp>.log; app<timestamp>.log shows Information and above.

Customizing

  • Change minimums per logger: Set-KrLoggerLevel -Value Warning on one logger.
  • Add properties per logger: Add-KrEnrichProperty -Name 'Logger' -Value 'audit'.
  • Add sinks independently: app to Seq, audit to file only.
  • To enable Kestrun framework logs, use -SetAsDefault on either logger or pass -Logger/-LoggerName to New-KrServer.

Troubleshooting

Symptom Cause Fix
No debug lines in app.log App minimum is Information Lower app minimum to Debug
Nothing in audit.log No events at/above minimum or bad path Trigger events; verify path and permissions
Duplicate console lines Multiple console sinks active Use one console sink or adjust configuration
Cannot delete/rename log file Logger still open / file handle Run Close-KrLogger; let the process exit

References


Previous / Next

Go back to Simple Logging or continue to Enrichment & Correlation IDs. Guide: Logging (Concepts)