Logging Configuration Guide
This document provides instructions for configuring logging in Application using Serilog via a SerilogConfig.json file embedded with the application build.
Overviewβ
- Application uses Serilog for structured logging.
- Logging targets (called sinks) and log levels are configured via
SerilogConfig.json. - The application is configured to support dynamic config reload for log levels at runtime when
SerilogConfig.jsonis updated. - Changing log targets (WriteTo section) still requires an application restart.
Note
The logging configuration and guidance described in this document apply to both Azure deployments and on-premises deployments of the application. Configuration steps, log targets, and runtime log level changes are supported in both environments.
π Configuration File Locationβ
The logging configuration is controlled through a SerilogConfig.json file placed within your application's build directory.
Logs are in compact JSON format.
π Available Logging Targets & Configuration Examplesβ
β Console & File Logsβ
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"Enrich": [ "FromLogContext", "WithThreadId" ],
"MinimumLevel": {
"Default": "Error",
"Override": {
"Microsoft": "Error",
"System": "Error"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
}
},
{
"Name": "File",
"Args": {
"path": "%LOG_DIR%/Log/log.json",
"shared": true,
"fileSizeLimitBytes": 26214400,
"rollingInterval": "Infinite",
"rollOnFileSizeLimit": true,
"retainedFileCountLimit": 10,
"retainedFileTimeLimit": null,
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
}
}
]
}
}
Note:
- The application sets the
%LOG_DIR%variable at runtime to ensure log files are written to the applicationβs base directory:
β Graylog Logsβ
{
"Serilog": {
"Using": [ "Serilog.Sinks.Graylog" ],
"Enrich": [ "FromLogContext", "WithThreadId" ],
"MinimumLevel": {
"Default": "Error",
"Override": {
"Microsoft": "Error",
"System": "Error"
}
},
"WriteTo": [
{
"Name": "Graylog",
"Args": {
"HostnameOrAddress": "http://localhost",
"Port": 12201,
"TransportType": "Udp",
"UseSsl": false,
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
}
}
]
}
}
βοΈ Update
HostnameOrAddress,Port,TransportType, andUseSslas per your Graylog server setup.
β Application Insights Logsβ
{
"Serilog": {
"Using": [ "Serilog.Sinks.ApplicationInsights" ],
"Enrich": [ "FromLogContext", "WithThreadId" ],
"MinimumLevel": {
"Default": "Error",
"Override": {
"Microsoft": "Error",
"System": "Error"
}
},
"WriteTo": [
{
"Name": "ApplicationInsights",
"Args": {
"connectionString": "%ProtrakApplicationInsightsConnectionString%",
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
}
]
}
}
βοΈ Ensure
%ProtrakApplicationInsightsConnectionString%is set as an environment variable on the host machine or environment.
π Changing Log Level at Runtimeβ
Application supports dynamically updating log levels at runtime via SerilogConfig.json without requiring a restart.
π To Update Log Level:β
- Open your
SerilogConfig.jsonfile. - Modify the
MinimumLevelsection like this:
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
}
- The application automatically detects changes in
SerilogConfig.jsonand applies the new log levels on the fly.
π Supported Log Levels (Case-Insensitive)β
When setting log levels via configuration or runtime input, the following mappings apply:
| Input Value | Resolved Log Level |
|---|---|
trace, verbose, verb | Verbose |
information, info | Information |
debug | Debug |
warning, warn | Warning |
error | Error |
fatal, critical | Fatal |
| (anything else) | Warning (default fallback) |
β οΈ Known Limitationsβ
- Changing logging targets (sinks) like switching from Console to Graylog or adding a new sink requires an application restart.
- Only log levels can be modified dynamically at runtime via the
SerilogConfig.jsondue to how Serilog reloads configurations.
| Action | Supported at Runtime? | Notes |
|---|---|---|
| π Change Log Level | β | Automatically applied when SerilogConfig.json changes |
| ββ Add/Remove/Change Log Targets | β | Requires application restart to take effect |
| π Update Application Insights Connection String | β | Needs restart to reload updated environment variable |
π Best Practicesβ
- Always verify environment variables (
%LOG_DIR%,%ProtrakApplicationInsightsConnectionString%) are correctly configured on your deployment environment. - When switching log targets (Console β File β Graylog β Application Insights), update
SerilogConfig.jsonand restart the application. - To modify verbosity (log levels) for troubleshooting, simply edit
SerilogConfig.jsonβ no restart needed.
π£ Importantβ
Use the following logging tables and practices for consistent observability:
- Exceptions Table: Capture all error logs.
- Traces Table: Capture diagnostic and trace logs.
- Use
customDimensions.correlationIdto track requests across services. - Always filter on
TenantIdin multi-tenant environments.
π Conclusionβ
Application offers flexible, externalized logging via Serilog.
While log levels can be updated at runtime, changing log destinations requires a restart for reliability and stability.