PvWay.LoggerService.nc6 4.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package PvWay.LoggerService.nc6 --version 4.0.0                
NuGet\Install-Package PvWay.LoggerService.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.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.nc6 --version 4.0.0                
#r "nuget: PvWay.LoggerService.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.nc6 as a Cake Addin
#addin nuget:?package=PvWay.LoggerService.nc6&version=4.0.0

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

pvWay Logger Service for dotNet core 6

Description

This nuget provides the base classes for several very intuitive LoggerService implementations of the PvWay.LoggerService.Abstractions.nc8 ILoggerService interface :

  • ConsoleLoggerService (IConsoleLoggerService)

    • This colorful implementation uses Console.WriteLine outputting logs to the standard out.
  • MsSqlLoggerService (IMsSqlLoggerService)

    • This implementation uses a DAO connection towards a Ms Sql Server Database that persist log rows into the table of your choice
  • MuteLoggerService (IMuteLoggerService)

    • As the name sounds this implementation can be used to injecting a silent logger. This can be handy for unit testing.
  • PgSqlLoggerService (IPgSqlLoggerService)

    • This implementation uses a DAO connection towards a PostgreSQL Database that persist log rows into the table of your choice
  • SeriConsoleLoggerService (ISeriConsoleLoggerService)

    • Uses the well known serilog(tm) console skin package
  • UTestLoggerService (IUTestLoggerService)

    • Unit testing implementation allowing you to perform asserts on logs content
  • hybridLoggerService (IHybridLoggerService)

    • Write simultaneously to multiple outputs such as a ConsoleLogger and an SqlLogger

MethodResultWrapper

Provides a generic wrapper that returns whether or not a method succeeded or failed carrying the method result on success or a list of notifications in case of failure.

MethodResult interfaces

public interface IMethodResult
{
    /// <summary>
    /// At least one notification has a severity
    /// greater or equal to Error
    /// </summary>
    bool Failure { get; }

    /// <summary>
    /// No notification or all notifications severity
    /// are lower than Error
    /// </summary>
    bool Success { get; }

    SeverityEnum Severity { get; }

    /// <summary>
    /// Bulk string made of the concatenation
    /// of the notifications separated by new
    /// lines
    /// </summary>
    string ErrorMessage { get; }

    IEnumerable<IMethodResultNotification> Notifications { get; }

    void AddNotification(string message, SeverityEnum severity);
    void AddNotification(IMethodResultNotification notification);

    /// <summary>
    /// Will throw new Exception(ErrorMessage)
    /// </summary>
    void Throw();
}

public interface IMethodResult<out T> : IMethodResult
{
    T? Data { get; }
}

public interface IMethodResultNotification
{
    SeverityEnum Severity { get; }
    string Message { get; }
}

Features

  • MethodResult (implementing IMethodResult) is a class that

    • returns whether or not a method succeeded, has fatal, errors or warnings
    • the returned object provides
      • a boolean property named Failure that will be set when at least on notification has a severity of error or fatal
      • a boolean property named Success that is simply equals to !Failure
      • a list of notifications (message and severity)
      • an ErrorMessage string (list of notifications separated by new lines)
      • a method that allows to throw an exception
  • MethodResult<T> (inheriting from IMethodResult) is a generic class that

    • returns an object of type T if the method succeeded

Usage

using System.Data;
using PvWay.LoggerService.Abstractions.nc6;
using PvWay.LoggerService.nc6;

namespace PvWay.MethodResultWrapperLab.nc6;

internal sealed class MethodResultWrapperDemo(
    ILoggerService ls,
    IUserStore userStore)
{
    public async Task<IMethodResult<string>> GetUserFirstNameAsync(
        string userName)
    {
        // let's call the GetUser Method and see its result
        // the method returns a IMethodResult<IUser> object
        var getUser = await GetUserAsync(userName);
        if (getUser.Failure)
        {
            // something wrong happened
            // let's log this and return a
            // MethodResult object that will carry
            // the notifications collected by the getUser method
            await ls.LogAsync(getUser);
            return new MethodResult<string>(getUser);
        }

        // the user was found
        // let's get the user object from the getUser.Data
        var user = getUser.Data!; // this returns an IUser

        var firstName = user.FirstName;

        // let's call the MethodResult success constructor
        // by passing the expected data type object (here 
        // a string)
        return new MethodResult<string>(firstName);
    }

    private async Task<IMethodResult<IUser>> GetUserAsync(
        string userName)
    {
        try
        {
            var user = await userStore.GetUserAsync(userName);
            if (user != null)
            {
                // the user was found
                // let's call the MethodResult success constructor
                // by passing the expected data type object (here 
                // a IUser object)
                return new MethodResult<IUser>(user);
            }

            // the user was not found...
            // this is a Business (non technical error)
            // let's construct a failure MethodResult object
            // with the Error (business error) severity
            var err = new MethodResult<IUser>(
                $"User {userName} not found", SeverityEnu.Error);

            // let's log this (business) error
            await ls.LogAsync(err);

            // let's return the MethodResult to the caller
            return err;
        }
        catch (Exception e)
        {
            // something raised an exception...
            // for example the data base might not be up
            // let's log this fatal error
            await ls.LogAsync(e);
            // let's construct and return a failure MethodResult
            // with the Fatal (technical error) severity
            // and the exception.
            return new MethodResult<IUser>(e);
        }
    }

}

internal interface IUser
{
    string FirstName { get; }
}

internal interface IUserStore
{
    Task<IUser?> GetUserAsync(string userName);
}

internal class UserStore : IUserStore
{
    public Task<IUser?> GetUserAsync(string userName)
    {
        throw new DataException();
    }
}

Output

14:13:48 <span style="background-color: red;">FTL</span> Exception: Data Exception.<BR> StackTrace: at PvWay.MethodResultWrapperLab.nc8.UserStore.GetUserAsync(String userName) in C:\gitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.MethodResultWrapperLab.nc8\MethodResultWrapperDemo.cs:line 96 at PvWay.MethodResultWrapperLab.nc8.MethodResultWrapperDemo.GetUserAsync(String userName) in C:\gitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.MethodResultWrapperLab.nc8\MethodResultWrapperDemo.cs:line 44 from LNV14A in GetUserAsync (C:\gitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.MethodResultWrapperLab.nc8\MethodResultWrapperDemo.cs) line 72 at 12/13/2023 13:13:48

14:13:48 <span style="background-color: red;">FTL</span> Fatal:Exception: Data Exception.<BR> StackTrace: at PvWay.MethodResultWrapperLab.nc8.UserStore.GetUserAsync(String userName) in C:\gitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.MethodResultWrapperLab.nc8\MethodResultWrapperDemo.cs:line 96 at PvWay.MethodResultWrapperLab.nc8.MethodResultWrapperDemo.GetUserAsync(String userName) in C:\gitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.MethodResultWrapperLab.nc8\Me thodResultWrapperDemo.cs:line 44 from LNV14A in GetUserFirstNameAsync (C:\gitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.MethodResultWrapperLab.nc8\MethodResultWrapperDemo.cs) line 23 at 12/13/2023 13:13:48

14:13:48 <span style="background-color: red;">FTL</span> Fatal:Exception: Data Exception.<BR> StackTrace: at PvWay.MethodResultWrapperLab.nc8.UserStore.GetUserAsync(String userName) in C:\gitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.MethodResultWrapperLab.nc8\MethodResultWrapperDemo.cs:line 96 at PvWay.MethodResultWrapperLab.nc8.MethodResultWrapperDemo.GetUserAsync(String userName) in C:\gitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.MethodResultWrapperLab.nc8\Me thodResultWrapperDemo.cs:line 44 from LNV14A in <Main>$ (C:\gitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.MethodResultWrapperLab.nc8\Program.cs) line 26 at 12/13/2023 13:13:48

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 (10)

Showing the top 5 NuGet packages that depend on PvWay.LoggerService.nc6:

Package Downloads
pvWay.MethodResultWrapper.nc6

Provides a generic wrapper that returns whether or not a method succeeded or failed carrying the method result on success or a list of notifications/errors in case of failure

PvWay.LoggerService.PgSqlLogWriter.nc6

PostgreSQL implementation of the pvWay.LoggerService.Abstractions.nc6 that persists logs into a table in a PostgreSQL database

PvWay.LoggerService.SeriConsole.nc6

Implements the ILoggerService as a simple stdout console using the well known Serilog(tm) console skin logger.

PvWay.LoggerService.MsSqlLogWriter.nc6

Microsoft SQL implementation of the pvWay.LoggerService.Abstractions.nc6 that persists logs into a table in a MsSQL database

PvWay.LoggerService.Console.nc6

Implements the ILoggerService as a simple stdout console using Console.WriteLine statements. The output is colored depending on the severity.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.1 261 8/12/2024
4.0.0 107 8/11/2024
3.0.4 233 8/4/2024
3.0.3 82 8/4/2024
3.0.2 181 8/4/2024
3.0.1 176 8/3/2024
3.0.0 172 8/2/2024
2.0.1 285 8/28/2023
2.0.0 177 8/25/2023
1.0.1 139 8/25/2023
1.0.0 226 6/7/2023

Now including MethodResult