Logging configuration in .NET core
.NET core introduces a logging API that works with a number of logging frameworks. The built-in providers are configurable and extensible to support different level loggings.
Built-in Logging Providers
Microsoft provides a number of built-in providers. There are two logging providers created in the project templates: Console and Debug.
Console writes logs to the console while Debug uses System.Diagnostics.Debug to write logs.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IDatabaseInitializer dbInitializer)
There are also other built-in providers like EventSource, EventLog, Azure App Service and TraceSource.
In the above example, it specifies Logging section in configuration file is configured for Console logging provider.
IncludeScopes indicates whether logging scope is supported.
Scope can be used to group a number of logical operations to attach the same data to each log created within that scope.
For more details, please refer to:
Different levels of logging are supported. As described in the following code, the most detailed level is Trace. In the configuration sample ‘"Default": "Warning"’, default logging level is Warning, which means only Warning, Error and Critical messages will be logged in Console.
// Defines logging severity levels.
public enum LogLevel
// Logs that contain the most detailed messages. These messages may contain sensitive
// application data. These messages are disabled by default and should never be
// enabled in a production environment.
Trace = 0,
// Logs that are used for interactive investigation during development. These logs
// should primarily contain information useful for debugging and have no long-term
Debug = 1,
// Logs that track the general flow of the application. These logs should have long-term
Information = 2,
// Logs that highlight an abnormal or unexpected event in the application flow,
// but do not otherwise cause the application execution to stop.
Warning = 3,
// Logs that highlight when the current flow of execution is stopped due to a failure.
// These should indicate a failure in the current activity, not an application-wide
Error = 4,
// Logs that describe an unrecoverable application or system crash, or a catastrophic
// failure that requires immediate attention.
Critical = 5,
// Not used for writing log messages. Specifies that a logging category should not
// write any messages.
None = 6
In this example above, the log levels are:
- Controller MyController: Error
- All the categories that begin with MyNamespace: Debug
- Default log level is Information (will be used when no filters match)
- Microsoft: only Warning messages will be logged.
Configure using Code
You can also use code in your Startup class to configure loggings and filters.