Logging - mcbride-clint/DeveloperCurriculum GitHub Wiki
Logging in .Net was another feature that Microsoft left out of .Net Framework and they left it in the hands of 3rd party Providers that was not completely integrated with the Framework. The best a developer could do without 3rd party libraries would be to utilize static and singleton variables to hold a collection of strings and write it out manually to a file.
To aid developers and 3rd party providers, Microsoft created a series of backward compatible Interfaces related to Logging.
ILogger
and ILoggerProvider
are two that will be most commonly come across.
A Host
object can have 1 or many Logging Providers added to it and when a new Log Message is created it will be sent to all of the added providers.
When a GenericHost with defaults is created the following providers are created:
- Console
- Debug
- EventSource
- EventLog: Windows only
A developer and 3rd parties can create their own providers that implement ILoggingProvider
and seamlessly hook into the .Net ecosystem.
Logging within the .Net Logging Library, relies pretty heavily on using Dependency Injection to provide and instance of ILogger<T>
.
public class AboutModel : PageModel
{
private readonly ILogger _logger;
public AboutModel(ILogger<AboutModel> logger)
{
_logger = logger;
}
public string Message { get; set; }
public void OnGet()
{
Message = $"About page visited at {DateTime.UtcNow.ToLongTimeString()}";
_logger.LogInformation(Message);
}
}
When this logging entry is created it will send something like the following to the registered Logging Providers.
info: AboutModel [1002] About page visited at 6/2/2021 02:11:31 AM
The name of the class that the ILogger<T>
T, is included in each log entry so that where the log message came from is easily found.
Most providers will also include the Timestamp as well.
Log Levels can be used to add further context, severity, and filtering to messages. Levels can be used to easily see which messages are important errors, warnings, or if they are just for informational purposes. Log messages can also be prevented from being sent out through log filtering in configuration entries. Log messages from particular namespaces and classes can be limited to a log level to increase or decrease verbosity.
{
"Logging": { // Default, all providers.
"LogLevel": {
"Microsoft": "Warning"
},
"Console": { // Console provider.
"LogLevel": {
"Microsoft": "Information"
}
}
}
}
Popular 3rd Party Logging Libraries include log4Net and Serilog. Both offer additional customization and features on top of the default providers.
- Microsoft Docs - Logging in .Net - https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0
- Microsoft Docs - Logging Without a Host - https://docs.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#non-host-console-app
- Pluralsight - .Net Logging with Serilog - https://app.pluralsight.com/library/courses/dotnet-logging-using-serilog-opinionated-approach/table-of-contents