ILogger
By default, ILogger
is included in an ASP.NET Core project.
Log Levels
There are six log levels available:
- Trace - The lowest level
- Debug
- Information
- Warning
- Error
- Critical - The highest level
Not all logs need to be processed or displayed in your console or database. You can configure the logger to only process logs above a certain level, filtering out less critical entries.
Logger Configuration
For development, you can configure logging settings in appsettings.Development.json
:
{
"Logging": {
"LogLevel": {
"Default": "Information", // Default log level for the application
"Microsoft.AspNetCore": "Warning" // Only display logs at level Warning or higher
}
}
}
This configuration ensures that logs for your application will display at least Information level, while logs from ASP.NET Core (e.g., framework-related logs) will be filtered to show Warning level and above.
Custom Configuration for Specific Classes
If you want to set a specific log level for a particular class or controller, you can add more specific entries in the .json
file. For example:
{
"Logging": {
"LogLevel": {
"WeatherAppAPI.Controllers.WeatherForecastController": "", // Specific to this controller
"WeatherAppAPI.Controllers": "", // Applies to all classes in this namespace
}
}
}
In this example, the log level for WeatherForecastController
is set to Debug, while all controllers in the WeatherAppAPI.Controllers
namespace will have a minimum level of Information.