Logging
When something behaves unexpectedly, logs are usually your first stop. Portway logs through Serilog, which gives you structured output, sensible file rotation, and per-namespace level control. Here is how the pieces fit together.
Log outputs
Console logging
- Displays real-time logs with timestamp formatting
- Information level and above shown by default
- Color-coded by severity level
- Useful for development and debugging
File logging
- Stored in the
/logdirectory - Daily rotation with pattern:
portwayapi-YYYYMMDD.log - 10MB file size limit with automatic rollover
- Retains 10 days of log files
- Buffered writing for performance
Log levels
| Level | Description | Examples |
|---|---|---|
| Debug | Detailed diagnostic information | Database queries, method execution |
| Information | Normal operational events | API requests, successful operations |
| Warning | Unexpected but handled situations | Missing configuration, fallback behavior |
| Error | Failures and exceptions | Database errors, API failures |
| Fatal | Critical failures | Application startup failures |
Configuration
Logging is configured in appsettings.json under the Serilog section. See Application Settings for the full configuration schema.
Log file management
Rotation policy
- Daily rotation at midnight
- Size-based rotation at 10MB
- Automatic file naming with date suffix
Retention policy
- Keeps last 10 log files
- Older files automatically deleted
- Configurable retention period
File naming convention
txt
log/
├── portwayapi-20240120.log
├── portwayapi-20240119.log
└── portwayapi-20240118.logPerformance logging
Request timing
txt
[DBG] Incoming request: POST /api/500/Orders
[DBG] Outgoing response: 200 for /api/500/Orders - Took 125msRate limiting
txt
[INF] Rate limiter initialized - IP: 100/60s, Token: 1000/60s
[INF] IP 192.168.1.100 has exceeded rate limit, blocking for 60s
[DBG] Rate limit for IP 192.168.1.100 has expired, allowing trafficStructured logging
Event properties
The logging system captures structured data for better analysis:
- Request method and path
- User identity and token information
- Environment and endpoint names
- Duration and status codes
- Error details and stack traces
Context enrichment
Logs are automatically enriched with:
- Machine name
- Application version
- Request correlation IDs
- User context
- Environment information
Troubleshooting
Diagnostic tools
Handy commands for log analysis:
Portway