Customizing Logging ASP.NET Core
This will show you how to customize the logging. The default logging only writes to the console or to the debug window. This is quite good for the most cases, but maybe you need to log to a sink like a file or a database. Maybe you want to extend the logger with additional information.
Category Fundamentals
Published: 16 April 2021
Configure logging
In previous versions of ASP.NET Core (pre 2.0) the logging was configured in the Startup.cs
. Since 2.0 the Startup.cs
was simplified and a lot of configurations where moved to a default WebHostBuilder
, which is called in the Program.cs
. Also the logging was moved to the default WebHostBuilder
:
In ASP.NET Core you are able to override and customize almost everything. So you can with the logging. The IWebHostBuilder
has a lot of extension methods to override the default behavior. To override the default settings for the logging we need to choose the ConfigureLogging
method. The next snippet shows exactly the same logging as it was configured inside the CreateDefaultBuilder()
method:
This method needs a lambda that gets a WebHostBuilderContext
that contains the hosting context and a LoggingBuilder
to configure the logging.
Create a custom logger
To demonstrate a custom logger, I created a small useless logger that is able to colorize log entries with an specific log level in the console. This so called ColoredConsoleLogger
will be added and created using a LoggerProvider we also need to write by our own. To specify the color and the log level to colorize, we need to add a configuration class. In the next snippet all three parts (Logger, LoggerProvider and Configuration) are shown:
We need to lock the actual console output, because we will get some race conditions where wrong log entries get colored with the wrong color, because the console itself is not really thread save.
If this is done we can start to plug in the new logger to the configuration:
If needed you are able to clear all the previously added logger providers. Than we call AddProvider
to add a new instance of our ColoredConsoleLoggerProvider
with the specific settings. We could also add some more instances of the provider with different settings.
This shows ho to handle different log levels in a a different way. You can use this to send an emails on hard errors, to log debug messages to a different log sink than regular informational messages and so on.
In many cases it doesn't make sense to write a custom logger because there are already many good third party loggers, like elmah, log4net and NLog. In the next section I'm going to show you how to use NLog in ASP.NET Core
Plug-in an existing Third-Party logger provider
NLog was one of the very first loggers, which was available as a .NET Standard library and usable in ASP.NET Core. NLog also already provides a Logger Provider to easily plug it into ASP.NET Core.
The next snippet shows a typical NLog.Config
that defines two different sinks to log all messages in one log file and custom messages only into another file:
We than need to add the NLog ASP.NET Core package from NuGet:
(Be sure you are in the project directory before you execute that command)
Now you only need to add NLog in the ConfigureLogging
method in the Program.cs
The first line configures NLog to use the previously created NLog.Config
and the second line adds the NLogLoggerProvider
to the list of logging providers. Here you can add as many logger providers you need.
Conclusion
The good thing of hiding the basic configuration is only to clean up the newly scaffolded projects and to keep the actual start as simple as possible. The developer is able to focus on the actual features. But the more the application grows the more important is logging. The default logging configuration is easy and it works like charm, but in production you need a persisted log to see errors from the past. So you need to add a custom logging or a more flexible logger like NLog or log4net.