Writing logs
Now that we understand what logs are, it is time to translate that knowledge into C#. Most of what we need is part of the Microsoft.Extensions.Logging.Abstractions
assembly, which is included by default in ASP.NET Core projects, so we don’t need to do anything to tap into the logging system. The Microsoft.Extensions.Logging
namespace contains most of the logging building blocks.
The logging system is provider-based, meaning we must register one or more ILoggerProvider
instances if we want our log entries to be recorded somewhere, like in stdout or a file. By default, when calling WebApplication.CreateBuilder(args)
, it registers the Console, Debug, EventSource, and EventLog (Windows only) providers, but we can modify this list. We can add and remove providers if you need to. The required dependencies for using the logging system are also registered as part of this process.
Before we look at the code, let’s learn how to create log entries, which is...