Sinks (Console/File/JSON)
Wire different outputs for your logs. This sample writes to:
- Console (human-friendly text)
- Rolling text file
logs/sinks-text<timestamp>.log - Rolling JSON file
logs/sinks-json<timestamp>.log
For more sinks (HTTP, EventLog, Syslog, Seq), see the Logging guide.
Full source
File: pwsh/tutorial/examples/5.4-Sinks.ps1
<#
Sample demonstrating sinks: console, text file, and JSON file.
- 'text' logger: writes to console + logs/sinks-text.log
- 'json' logger: writes to logs/sinks-json.log with JSON formatter
FileName: 5.4-Sinks.ps1
#>
param(
[int]$Port = 5000,
[IPAddress]$IPAddress = [IPAddress]::Loopback
)
$text = New-KrLogger |
Set-KrLoggerLevel -Value Information |
Add-KrSinkConsole |
Add-KrSinkFile -Path '.\logs\sinks-text.log' -RollingInterval Hour |
Register-KrLogger -Name 'text' -PassThru
$json = New-KrLogger |
Set-KrLoggerLevel -Value Debug |
Add-KrSinkFile -Path '.\logs\sinks-json.log' -Formatter (Get-KrSinkJsonFormatter) -RollingInterval Hour |
Register-KrLogger -Name 'json' -PassThru
New-KrServer -Name "Sinks Demo"
Add-KrEndpoint -Port $Port -IPAddress $IPAddress
Enable-KrConfiguration
Add-KrMapRoute -Verbs Get -Path "/text" -ScriptBlock {
Write-KrLog -Logger $text -Level Information -Message "Text sink example"
Write-KrTextResponse -InputObject "text" -StatusCode 200
}
Add-KrMapRoute -Verbs Get -Path "/json" -ScriptBlock {
Write-KrLog -Logger $json -Level Debug -Message "Json sink example" -Values @{ Example = $true }
Write-KrTextResponse -InputObject "json" -StatusCode 200
}
# Start the server and close all the loggers when the server stops
# This is equivalent to calling Close-KrLogger after Start-KrServer
Start-KrServer -CloseLogsOnExit
Step-by-step
- Create
textlogger with minimumInformation, add Console + File sinks. - Create
jsonlogger with minimumDebug, add File sink withGet-KrJsonFormatter. - Standard server + listener + PowerShell runtime.
- Apply config: call
Enable-KrConfigurationto commit the listener/runtime. - Routes:
/textwrites an Information event via thetextlogger./jsonwrites a Debug event with an extra property via thejsonlogger.
- Start:
Start-KrServerbegins processing requests.
Try it
Save the sample locally so the log files are easy to find. Copy the contents of pwsh/tutorial/examples/5.4-Sinks.ps1 into a new file in an empty working folder (for example, sinks-demo.ps1), then run:
# From your working folder
pwsh .\sinks-demo.ps1
curl http://127.0.0.1:5000/text
curl http://127.0.0.1:5000/json
Get-Content .\logs\sinks-text*.log -Tail 20
Get-Content .\logs\sinks-json*.log -Tail 20
Expected: sinks-text*.log contains human-readable events; sinks-json<timestamp>.log contains JSON entries.
Customizing
- Restrict console to higher level:
Add-KrSinkConsole -RestrictedToMinimumLevel Warning. - Change rolling interval or retention on file sinks.
- Add HTTP/Syslog/EventLog sinks; add structured properties.
- Set one logger as default or pass to
New-KrServerto enable Kestrun framework logs.
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Empty JSON file | No Debug events written | Lower minimum for json or send Debug events |
| Garbled console output | Custom template mismatch | Remove/adjust OutputTemplate |
| File not created | Path/permissions/empty stream | Check path; trigger requests |
| Cannot delete/rename log file | Logger still open / file handle | Run Close-KrLogger; let the process exit |
References
- New-KrLogger
- Set-KrLoggerLevel
- Get-KrJsonFormatter
- Add-KrSinkFile
- Add-KrSinkConsole
- Register-KrLogger
- Write-KrLog
- Close-KrLogger
- New-KrServer
- Add-KrMapRoute
- Start-KrServer
Previous / Next
Go back to Enrichment & Correlation IDs or continue to Sinks (HTTP/EventLog/Syslog). Guide: Logging (Concepts)