PvWay.LoggerService.Console.nc6 4.0.0

dotnet add package PvWay.LoggerService.Console.nc6 --version 4.0.0                
NuGet\Install-Package PvWay.LoggerService.Console.nc6 -Version 4.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="PvWay.LoggerService.Console.nc6" Version="4.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PvWay.LoggerService.Console.nc6 --version 4.0.0                
#r "nuget: PvWay.LoggerService.Console.nc6, 4.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 PvWay.LoggerService.Console.nc6 as a Cake Addin
#addin nuget:?package=PvWay.LoggerService.Console.nc6&version=4.0.0

// Install PvWay.LoggerService.Console.nc6 as a Cake Tool
#tool nuget:?package=PvWay.LoggerService.Console.nc6&version=4.0.0                

pvWay ConsoleLoggerService for dotNet Core 6

Description

This nuget implements the ILoggerService as a simple stdout console using Console.WriteLine statements.

The output is colored depending on the severity.

Severity enum

public enum SeverityEnu
{
    Ok,         // "O" Green
    Trace,      // "T" Gray
    Debug,      // "D" White
    Info,       // "I" DarkCyan
    Warning,    // "W" DarkYellow
    Error,      // "E" DarkRed
    Fatal       // "F" Red
}

Methods

All methods include both a synchronous and an asynchronous signature.

The class also implement the ILogger interface (microsoft.logging)

See here after the main methods:

void Log(
    string message,
    SeverityEnu severity = SeverityEnu.Debug,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string filePath = "",
    [CallerLineNumber] int lineNumber = -1);

Task LogAsync(
    string message,
    SeverityEnu severity = SeverityEnu.Debug,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string filePath = "",
    [CallerLineNumber] int lineNumber = -1);
        
void Log(
    IEnumerable<string> messages,
    SeverityEnu severity,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string filePath = "",
    [CallerLineNumber] int lineNumber = -1);

Task LogAsync(
    IEnumerable<string> messages,
    SeverityEnu severity,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string filePath = "",
    [CallerLineNumber] int lineNumber = -1);

void Log(
    Exception e,
    SeverityEnu severity = SeverityEnu.Fatal,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string filePath = "",
    [CallerLineNumber] int lineNumber = -1);

Task LogAsync(
    Exception e,
    SeverityEnu severity = SeverityEnu.Fatal,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string filePath = "",
    [CallerLineNumber] int lineNumber = -1);

void Log(
    IMethodResult result,
    string? topic,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string filePath = "",
    [CallerLineNumber] int lineNumber = -1);

Task LogAsync(
    IMethodResult result,
    string? topic,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string filePath = "",
    [CallerLineNumber] int lineNumber = -1);

Injection

The AddPvWayConsoleLoggerService method extends the IServiceCollection

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using PvWay.LoggerService.Abstractions.nc6;
using PvWay.LoggerService.nc6;

namespace PvWay.LoggerService.Console.nc6;

public static class PvWayConsoleLogger
{
    // LOGGER PROVIDER
    public static ILoggerProvider GetProvider(
        SeverityEnu minLogLevel = SeverityEnu.Trace)
    {
        return new ConsoleLoggerProvider(minLogLevel);
    }
    
    // CREATORS
    public static IConsoleLogWriter CreateWriter()
    {
        return new ConsoleLogWriter();
    }

    public static IConsoleLoggerService CreateService(
        SeverityEnu minLogLevel = SeverityEnu.Trace)
    {
        return new ConsoleLoggerService(
            new LoggerServiceConfig(minLogLevel),
            new ConsoleLogWriter());
    }

    public static IConsoleLoggerService<T> CreateService<T>(
        SeverityEnu minLogLevel = SeverityEnu.Trace)
    {
        return new ConsoleLoggerService<T>(
            new LoggerServiceConfig(minLogLevel),
            new ConsoleLogWriter());
    }

   // WRITER 
    public static void AddPvWayConsoleLogWriter(
        this IServiceCollection services)
    {
        services.TryAddSingleton<IConsoleLogWriter, ConsoleLogWriter>();
    }
   
    // FACTORY
    public static void AddPvWayConsoleLoggerFactory(
        this IServiceCollection services)
    {
        services.AddSingleton<
            ILoggerServiceFactory<IConsoleLoggerService>,
            ConsoleLoggerServiceFactory>();
    }
    
    // SERVICE
    public static void AddPvWayConsoleLoggerService(
        this IServiceCollection services,
        SeverityEnu minLogLevel = SeverityEnu.Trace)
    {
        services.TryAddSingleton<ILoggerServiceConfig>(_ =>
            new LoggerServiceConfig(minLogLevel));
        
        services.AddPvWayConsoleLogWriter();

        const ServiceLifetime lifetime = ServiceLifetime.Singleton;
        var descriptors = new List<ServiceDescriptor>
        {
            new(typeof(IConsoleLoggerService),
                typeof(ConsoleLoggerService),
                lifetime),
            new(typeof(IConsoleLoggerService<>),
                typeof(ConsoleLoggerService<>),
                lifetime)
        };
        foreach (var sd in descriptors)
        {
            services.TryAdd(sd);
        }
    }   
}

Usage

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PvWay.LoggerService.Abstractions.nc8;
using PvWay.LoggerService.Console.nc8;

Console.WriteLine("Hello, ConsoleLoggerService");
Console.WriteLine();

var services = new ServiceCollection();
services.AddPvWayConsoleLoggerService();
var sp = services.BuildServiceProvider();
var ls = sp.GetRequiredService<IConsoleLoggerService>();

ls.Log("This is a trace test log message", SeverityEnu.Trace);
ls.Log("This is a debug test log message");
ls.Log("This is an info test log message", SeverityEnu.Info);
ls.Log("This is a warning test log message", SeverityEnu.Warning);
ls.Log("This is an error test log message", SeverityEnu.Error);
ls.Log("This is a fatal test log message", SeverityEnu.Fatal);

ls.Log(LogLevel.Trace, "MsLog trace");

Happy coding

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
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
4.0.0 110 8/12/2024
3.0.4 105 8/11/2024
3.0.3 77 8/4/2024
3.0.2 76 8/4/2024
3.0.1 69 8/3/2024
3.0.0 72 8/2/2024

Uses LoggerService 1.0.4