CtxLog.Abstractions 0.1.0

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

// Install CtxLog.Abstractions as a Cake Tool
#tool nuget:?package=CtxLog.Abstractions&version=0.1.0

CtxLog.Abstractions

Logging is more useful when you apply structured logging. Structured logging means that every log event adds custom properties depending on de context of the application or component. To make sure you use the same properties everytime, type-safe context objects give control over this.

Log Context

A Log Context is a situation where certain properties in the logging are expected. For instance, when you are in an order system, you may want to log what the status is of the order, the order number, numbers of items in the order and a client reference id. These are properties you may want to log.

Using this libary, you can create an object using these properties. By adding methods for the builder pattern, this library helps you to easily create a log context in the order handling component.

E.g.:

public class OrderLogContext : LogContext
    {
        public OrderLogContext()
        { }

        public string OrderStatus { get; set; }
        public int OrderNumber { get; set; }
        public int OrderLines { get; set; }
        public string ClientReference { get; set; }
        public override string ContextPrefix { get; protected set; } = "OrderInfo";

        protected override void AddPropertiesToContext()
        {
            base.AddPropertiesToContext();
            AddStringProperty(nameof(OrderStatus), OrderStatus);
            AddProperty(nameof(OrderNumber), OrderNumber);
            AddProperty(nameof(OrderLines), OrderLines);
            AddStringProperty(nameof(ClientReference), ClientReference);
        }
    }

Using this log context would be like this:

// You can also build up the context along the method
var ctx = LogContext.BuildLogContext<OrderLogContext>()
             .WithOrderNumber(2200152)
             .WithOrderLines(3)
             .WithStatus("InCart")
             .WithClientRef("C22XAGSF")
             .Context;
         using (logger.BeginScope(ctx) 
         {
             logger.LogInformation("Sending user to payment page");
         }
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
0.1.0 201 6/23/2022

First version. Feedback is welcome!