📎 Webclip
Logging Best Practices in ASP.NET Core
The post lays out practical logging guidance for ASP.NET Core applications. It favors Serilog over the built-in provider, uses log levels and filters to control volume, and treats structured logging as the default so fields stay searchable. It also stresses keeping sensitive data out of logs, capturing errors with context, and managing size and performance with rotation, retention, and async sinks. A centralized UI such as Seq is presented as a way to search, visualize, and alert on logs.
Reading notes#
- Serilog is recommended because it is performant, supports structured logging, and works on top of Microsoft.Extensions.Logging.
- Logging levels should match message importance, with Information or Warning as the default and Debug or Trace enabled only when needed.
- Filters can lower log volume by setting different minimum levels for namespaces such as Microsoft, OpenTelemetry, Quartz, and ASP.NET Core MVC.
- Structured logging keeps values like OrderId, ShipmentNumber, and State as searchable fields instead of plain text.
- String interpolation should be avoided in log calls because it turns structured data into unsearchable text.
- Sensitive information such as passwords, credit card numbers, API keys, authentication tokens, and connection strings should not be logged.
- Serilog can mask properties through destructuring policies, redact values manually, or exclude log events with filters.
- Errors should be logged with exception details and contextual data such as UserId and RequestPath, while stack traces should not be exposed to users.
- Log growth should be controlled with daily rotation, retention limits, file size limits, and asynchronous sinks for file logging.
- Console logging should be turned off in production.
- A centralized logging UI such as Seq can aggregate logs, support searches on structured fields, and enable alerts.
- The post also lists alternatives such as ELK Stack, Datadog, New Relic, Loggly, GrayLog, Azure Monitor Logs, and Amazon CloudWatch Logs.
