Darzid.Logging.Console 2.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Darzid.Logging.Console --version 2.0.2                
NuGet\Install-Package Darzid.Logging.Console -Version 2.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Darzid.Logging.Console" Version="2.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Darzid.Logging.Console --version 2.0.2                
#r "nuget: Darzid.Logging.Console, 2.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Darzid.Logging.Console as a Cake Addin
#addin nuget:?package=Darzid.Logging.Console&version=2.0.2

// Install Darzid.Logging.Console as a Cake Tool
#tool nuget:?package=Darzid.Logging.Console&version=2.0.2                

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:

  1. MyProcessorApp: Started
  2. MyProcessorApp: Creating Document Processor
  3. MyProcessorApp: Starting Document Processor
  4. MyProcessorApp\DocumentProcessor: Loading documents
  5. MyProcessorApp\DocumentProcessor: Processing documents
  6. MyProcessorApp\DocumentProcessor\Document1: Started processing document
  7. MyProcessorApp\DocumentProcessor\Document1: Calling webservice
  8. MyProcessorApp\DocumentProcessor\Document1: Calling another webservice
  9. MyProcessorApp\DocumentProcessor\Document1: Saving document
  10. MyProcessorApp\DocumentProcessor\Document2: Started processing document
  11. MyProcessorApp\DocumentProcessor\Document2: Calling webservice
  12. MyProcessorApp\DocumentProcessor\Document2: Calling another webservice
  13. MyProcessorApp\DocumentProcessor\Document2: Warning: Didn't get expected response
  14. MyProcessorApp\DocumentProcessor\Document2: Saving document
  15. MyProcessorApp\DocumentProcessor: Processing done
  16. MyProcessorApp: Finished

However, with rewriting enabled, it will look

  1. MyProcessorApp: Started / Creating Document Processor / Starting Document Processor (so, this single line will be updated 3 times)
  2. MyProcessorApp\DocumentProcessor: Loading documents / Processing documents (2 updates)
  3. MyProcessorApp\DocumentProcessor\Document1: Started processing document / Calling webservice / Calling another webservice / Saving document
  4. MyProcessorApp\DocumentProcessor\Document2: Started processing document / Calling webservice / Calling another webservice
  5. MyProcessorApp\DocumentProcessor\Document2: Warning: Didn't get expected response
  6. MyProcessorApp\DocumentProcessor\Document2: Saving document
  7. MyProcessorApp\DocumentProcessor: Processing done
  8. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.6 386 8/17/2021
2.0.5 356 6/15/2021
2.0.4 368 6/15/2021
2.0.3 593 1/24/2021
2.0.2 441 1/22/2021
2.0.1 409 1/18/2021
2.0.0 324 1/17/2021
1.0.3 411 1/16/2021
1.0.0 335 1/16/2021