Overview
ℹ️ Important
Availability: .NET 5
Documentation: https://learn.microsoft.com/en-us/dotnet/core/extensions/console-log-formatter
The Console
log provider supports three formatting options: Simple, Systemd, and Json.
Registering
To register any one of the formatters, use the Add{TYPE}Console
extension method:
using Microsoft.Extensions.Logging;
using ILoggerFactory loggerFactory =
LoggerFactory.Create(builder =>
builder.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "HH:mm:ss ";
}));
Registering via Configuration File
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"Console": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"FormatterName": "json",
"FormatterOptions": {
"SingleLine": true,
"IncludeScopes": true,
"TimestampFormat": "HH:mm:ss ",
"UseUtcTimestamp": true,
"JsonWriterOptions": {
"Indented": true
}
}
}
},
"AllowedHosts": "*"
}
Simple
- Can wrap information such as time and log level in each log message
- Supports ANSI color embedding
- Supports indentation fo messages
Systemd
- Uses the syslog standard
- Always logs messages on a single line
- No color support
Json
- Writes logs in JSON format
- By default, logs messages on a single line
- Use
JsonWriterOptions.Indented = true
to improve readability
- Use
Do not pass in log messages that have already been serialized to JSON
Custom Formatters
To create a custom formatter, inherit from ConsoleFormatter and register the custom formatter.
Documentation: https://learn.microsoft.com/en-us/dotnet/core/extensions/console-log-formatter#implement-a-custom-formatter