Lunet.Extensions.Logging.SpectreConsole 1.0.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
.NET Standard 2.0
dotnet add package Lunet.Extensions.Logging.SpectreConsole --version 1.0.0
NuGet\Install-Package Lunet.Extensions.Logging.SpectreConsole -Version 1.0.0
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="Lunet.Extensions.Logging.SpectreConsole" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Lunet.Extensions.Logging.SpectreConsole --version 1.0.0
#r "nuget: Lunet.Extensions.Logging.SpectreConsole, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Lunet.Extensions.Logging.SpectreConsole as a Cake Addin
#addin nuget:?package=Lunet.Extensions.Logging.SpectreConsole&version=1.0.0

// Install Lunet.Extensions.Logging.SpectreConsole as a Cake Tool
#tool nuget:?package=Lunet.Extensions.Logging.SpectreConsole&version=1.0.0

Lunet.Extensions.Logging.SpectreConsole

Build Status Coverage Status NuGet

A highly configurable Spectre.Console logger for Microsoft.Extensions.Logging.

Features

  • Highly configurable
  • Simple dependency to Microsoft.Extensions.Logging/6.0.0+ and Spectre.Console/0.44+
  • Add log markup methods (e.g LogInformationMarkup, LogWarningMarkup...) that can take additional Spectre.Console renderable objects.
    • Compatible with other loggers. Ansi colors will be removed from output.
  • Compatible with netstandard2.0+

Usage

On a logger factory configuration, you can simply configure Spectre console via the extension method configure.AddSpectreConsole()

Example 1

using Lunet.Extensions.Logging.SpectreConsole;
using Microsoft.Extensions.Logging;

// Example1: default layout (Similar to SimpleConsole)
using (var factory = LoggerFactory.Create(configure => configure.AddSpectreConsole()))
{
    var logger = factory.CreateLogger("SampleCategory");
    logger.LogInformationMarkup("Hello with [red]SpectreConsole[/]");
    logger.LogWarning("Hello without markup");
}

It will generate the following log:

Example 1

Example 2

using Lunet.Extensions.Logging.SpectreConsole;
using Microsoft.Extensions.Logging;

// Example2: Don't add a new line, include timestamp
using (var factory = LoggerFactory.Create(configure =>
           {
               configure.AddSpectreConsole(new SpectreConsoleLoggerOptions()
               {
                   IncludeNewLineBeforeMessage = false,
                   IncludeTimestamp = true,
               });
           }
       ))
{
    var logger = factory.CreateLogger("SampleCategory");
    logger.LogInformationMarkup(new EventId(1), "Hello from [red]SpectreConsole[/]");
    logger.LogWarning(new EventId(2), "Hello without markup");
}

It will generate the following log:

Example 2

Example 3

using Lunet.Extensions.Logging.SpectreConsole;
using Microsoft.Extensions.Logging;
using Spectre.Console;

// Example3: Don't add a new line, include timestamp, log a table
using (var factory = LoggerFactory.Create(configure =>
           {
               configure.AddSpectreConsole(new SpectreConsoleLoggerOptions()
               {
                   IncludeNewLineBeforeMessage = false,
                   IncludeTimestamp = true,
               });
           }
       ))
{

    var table = new Table();
    table.AddColumn("Name");
    table.AddColumn("Spectre?");
    table.AddRow("Microsoft.Extensions.Logging.Console", "⛔");
    table.AddRow("Lunet.Extensions.Logging.SpectreConsole", "✅");
    
    var logger = factory.CreateLogger("SampleCategory");
    logger.LogInformationMarkup(new EventId(1), "Hello from [red]SpectreConsole[/] with a table:", table);
    logger.LogWarning(new EventId(2), "Hello without markup");
}

It will generate the following log:

Example 3

Configuration

You can configure the way a log line will be displayed via SpectreConsoleLoggerOptions:

Property Type Description
LogLevel LogLevel The minimum log level to log. Default is Information
ConfigureConsole Action<IAnsiConsole> A callback to allow to configure the console once created from ConsoleSettn
ConsoleSettings AnsiConsoleSettings The settings of the console.
IncludeTimestamp bool A boolean indicating if the log should include a timestamp. Default is false.
TimestampFormat string The formatting string for the timestamp. Default is yyyy/MM/dd HH:mm:ss.fff.
EventIdFormat string The formatting string for the EventId. Default is ####.
CultureInfo CultureInfo The culture used for formatting. Default is invariant.
IncludeLogLevel bool A boolean indicating if the log should include the log level. Default is true.
IncludeCategory bool A boolean indicating if the log should include the log category. Default is true.
IncludeEventId bool A boolean indicating if the log should include the log event id. Default is true.
IncludeNewLineBeforeMessage bool A boolean indicating if the log should include a new line right before the message. Default is true<.
IndentAfterNewLine bool A boolean indicating if the log should indent on new lines. Default is true.
UseFixedIndent bool A boolean indicating if the log should use a fix indent on new lines instead of the automatic indent. Default is false<.
FixedIndent int The fixed indent level if UseFixedIndent is true.
LogException bool A boolean indicating whether to log exceptions to the output. Default is false.
SingleLine bool A boolean indicating if the log message should be emitted on a single line. Default is false. Note that if IncludeNewLineBeforeMessage, a new line will still be emitted before the log message.
GetLogTimeStamp delegate The callback to get the log timestamp. Default is Datetime.Now.
Formatter delegate The formatter used to format the datetime, log level, event id and category.
TimestampFormatter delegate The formatter for the timestamp of a log.
LogLevelFormatter delegate The formatter for the log level.
EventIdFormatter delegate The formatter for the log event id.
CategoryFormatter delegate The formatter for the log category.

License

This software is released under the BSD-Clause 2 license.

Author

Alexandre Mutel aka xoofx.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Lunet.Extensions.Logging.SpectreConsole:

Repository Stars
xoofx/dotnet-releaser
Easily build, run tests and coverage, cross-compile, package and publish your .NET library or application to NuGet and GitHub.
Version Downloads Last updated
1.0.0 592 4/2/2022