Darzid.Logging.Console
2.0.1
See the version list below for details.
dotnet add package Darzid.Logging.Console --version 2.0.1
NuGet\Install-Package Darzid.Logging.Console -Version 2.0.1
<PackageReference Include="Darzid.Logging.Console" Version="2.0.1" />
paket add Darzid.Logging.Console --version 2.0.1
#r "nuget: Darzid.Logging.Console, 2.0.1"
// Install Darzid.Logging.Console as a Cake Addin #addin nuget:?package=Darzid.Logging.Console&version=2.0.1 // Install Darzid.Logging.Console as a Cake Tool #tool nuget:?package=Darzid.Logging.Console&version=2.0.1
Overview
This console logger is a MS.Extensions.Logging compatible logger, which supports:
- Smart line rewrites, to keep console logs compact.
- A "breadcrumb" of scopes, like a folder path, in a configurable color
- Argument coloring.
Line rewrites
When logging a "normal" message (not a Warning, Error or Critical) from within the same scope, it updates the existing console line, instead of writing a new line. This means that it acts more like a "progress overview", instead of 1000's of lines zooming by on your console screen.
For example: Without rewriting, a console log line could look like this:
- MyProcessorApp: Started
- MyProcessorApp: Creating Document Processor
- MyProcessorApp: Starting Document Processor
- MyProcessorApp\DocumentProcessor: Loading documents
- MyProcessorApp\DocumentProcessor: Processing documents
- MyProcessorApp\DocumentProcessor\Document1: Started processing document
- MyProcessorApp\DocumentProcessor\Document1: Calling webservice
- MyProcessorApp\DocumentProcessor\Document1: Calling another webservice
- MyProcessorApp\DocumentProcessor\Document1: Saving document
- MyProcessorApp\DocumentProcessor\Document2: Started processing document
- MyProcessorApp\DocumentProcessor\Document2: Calling webservice
- MyProcessorApp\DocumentProcessor\Document2: Calling another webservice
- MyProcessorApp\DocumentProcessor\Document2: Warning: Didn't get expected response
- MyProcessorApp\DocumentProcessor\Document2: Saving document
- MyProcessorApp\DocumentProcessor: Processing done
- MyProcessorApp: Finished
However, with rewriting enabled, it will look
- MyProcessorApp: Started / Creating Document Processor / Starting Document Processor (so, this single line will be updated 3 times)
- MyProcessorApp\DocumentProcessor: Loading documents / Processing documents (2 updates)
- MyProcessorApp\DocumentProcessor\Document1: Started processing document / Calling webservice / Calling another webservice / Saving document
- MyProcessorApp\DocumentProcessor\Document2: Started processing document / Calling webservice / Calling another webservice
- MyProcessorApp\DocumentProcessor\Document2: Warning: Didn't get expected response
- MyProcessorApp\DocumentProcessor\Document2: Saving document
- MyProcessorApp\DocumentProcessor: Processing done
- MyProcessorApp: Finished
Although this is a simplified example, it reduces the number of lines with 50% (from 16 to 8).
In certain situations, a message is always written on a new line.
- When the LogLevel is Warning, Error or Critical, that particular line is written as a new line, so the last log message BEFORE the warning, error or critical log message is still displayed on screen. And the subsequent log message that is written AFTER the warning, error or critical log message is also written as a new line, so that the warning, error or critical log message remains visible.
- When the scope of the previous log message and the current log message is different, the message is written as a new line. So, the last message of the previous scope will remain visible.
- When some other logger or code wrote to the Console between the previous log message and the current log message. The Logger then continues writing below the messages that were written to the console outside of the Logger. So, when some code directly uses Console.WriteLine to write 5 lines, the logger will continue below the 5th line.
Scope Breadcrumb
As shown in the previous section, the logger displays a "breadcrumb" of current scopes, like a folder path: MyProcessorApp\DocumentProcessor\Document2
To achieve this, just supply the name of the scope when calling logger.BeginScope():
using (logger.BeginScope("Scope 1"))
{
logger.LogInformation("Log message: {param}", i);
}
Argument coloring
Microsoft.Extensions.Logging expects arguments to be specified in log messages by using { and }, like this:
logger.LogInformation("RegisterLoggerTest started at {timeStamp} on server {serverName}", DateTime.Now, serverName);
The Logger parses the log message, and splits the message into separate parts (static text and argument values), and displays the argument values in a configurable color. This makes it much easier to distinguish between the static text and the argument values.
Color configuration
- Breadcrumb color
- Message color: Information message color / Warning message color / Error message color
- Argument color
Register / create logger
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
builder.AddConsoleRewriteLogger(options =>
{
options.LogLevel = LogLevel.Information;
}));
ILogger<Program> logger = loggerFactory.CreateLogger<Program>();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Microsoft.Extensions.Logging (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.