Pepelax.Extensions.Logging.SpectreConsole 1.0.0

dotnet add package Pepelax.Extensions.Logging.SpectreConsole --version 1.0.0                
NuGet\Install-Package Pepelax.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="Pepelax.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 Pepelax.Extensions.Logging.SpectreConsole --version 1.0.0                
#r "nuget: Pepelax.Extensions.Logging.SpectreConsole, 1.0.0"                
#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 Pepelax.Extensions.Logging.SpectreConsole as a Cake Addin
#addin nuget:?package=Pepelax.Extensions.Logging.SpectreConsole&version=1.0.0

// Install Pepelax.Extensions.Logging.SpectreConsole as a Cake Tool
#tool nuget:?package=Pepelax.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.

Replace "Lunet" to use this fork This fork is created mainly to use WriteException method from SpectreConsole to make exceptions more readable

Features

  • Highly configurable
  • Simple dependency to Microsoft.Extensions.Logging/7.0.0+ and Spectre.Console/0.47+
  • 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 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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
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
1.0.0 116 10/13/2024