Section 2 of 6
Log Levels
🎯 What You'll Learn
- What are log levels
- Six log levels
- When to use each level
- Configuring log levels
- Filtering logs
What are Log Levels?
Log levels indicate the severity or importance of a log message. They help filter logs and focus on what matters.
Six Log Levels
| Level | Value | Description | When to Use |
|---|---|---|---|
| Trace | 0 | Most detailed | Detailed debugging, rarely used |
| Debug | 1 | Debugging info | Development debugging |
| Information | 2 | General info | Normal application flow |
| Warning | 3 | Unexpected events | Recoverable issues |
| Error | 4 | Errors/exceptions | Operation failures |
| Critical | 5 | Critical failures | Application crashes |
When to Use Each Level
Trace
Very detailed information, typically only enabled during development.
Example
C#
_logger.LogTrace("Entering method {MethodName}", nameof(GetProducts));
Debug
Information useful for debugging.
Example
C#
_logger.LogDebug("Query returned {Count} products", products.Count);
Information
General application flow events.
Example
C#
_logger.LogInformation("User {UserId} logged in", userId);
Warning
Unexpected but recoverable events.
Example
C#
_logger.LogWarning("Product {ProductId} stock is low: {Stock}", id, stock);
Error
Errors and exceptions that stop the current operation.
Example
C#
_logger.LogError(ex, "Failed to save product {ProductId}", productId);
Critical
Critical failures requiring immediate attention.
Example
C#
_logger.LogCritical("Database connection failed");
Configuring Log Levels
appsettings.json
JSON
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"InvenTrack.Controllers": "Debug"
}
}
}
appsettings.Development.json
JSON
{
"Logging": {
"LogLevel": {
"Default": "Debug"
}
}
}
Key Takeaways
- Six levels: Trace, Debug, Information, Warning, Error, Critical
- Trace/Debug: Development only
- Information: Normal flow
- Warning: Unexpected but recoverable
- Error: Operation failures
- Critical: Application crashes
- Configure: appsettings.json per environment