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
- Create the ‘app’ logger with minimum
Information, Console + File sinks, then register it asapp. - Create the ‘audit’ logger with minimum
Debug, File sink only, register asaudit. - Standard server + listener + PowerShell runtime.
- Apply configuration with
Enable-KrConfigurationto commit the listener/runtime. - Routes:
/infowrites Information toapp./debugwrites Debug to both loggers;appfilters Debug (not written),auditwrites it./auditdemonstrates an Information event onaudit.
- 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 Warningon one logger. - Add properties per logger:
Add-KrEnrichProperty -Name 'Logger' -Value 'audit'. - Add sinks independently:
appto Seq,auditto file only. - To enable Kestrun framework logs, use
-SetAsDefaulton either logger or pass-Logger/-LoggerNametoNew-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
- New-KrLogger
- Set-KrLoggerLevel
- Add-KrSinkFile
- Add-KrSinkConsole
- Register-KrLogger
- Write-KrLog
- Close-KrLogger
- New-KrServer
- Add-KrMapRoute
- Start-KrServer
Previous / Next
Go back to Simple Logging or continue to Enrichment & Correlation IDs. Guide: Logging (Concepts)