DevElf.Logging 0.4.3-alpha.0

This is a prerelease version of DevElf.Logging.
dotnet add package DevElf.Logging --version 0.4.3-alpha.0
                    
NuGet\Install-Package DevElf.Logging -Version 0.4.3-alpha.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="DevElf.Logging" Version="0.4.3-alpha.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DevElf.Logging" Version="0.4.3-alpha.0" />
                    
Directory.Packages.props
<PackageReference Include="DevElf.Logging" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DevElf.Logging --version 0.4.3-alpha.0
                    
#r "nuget: DevElf.Logging, 0.4.3-alpha.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.
#:package DevElf.Logging@0.4.3-alpha.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DevElf.Logging&version=0.4.3-alpha.0&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=DevElf.Logging&version=0.4.3-alpha.0&prerelease
                    
Install as a Cake Tool

DevElf.Logging

Advanced logging utilities that extend Microsoft.Extensions.Logging with disposable message scopes, property accumulation, and strict LIFO enforcement.

This library is currently in development and is not ready for production use.

The API is subject to breaking changes without notice. Features may be incomplete or unstable. Use at your own risk in non-production environments only.

NuGet Version

Features

  • Disposable log message scopes - Create scopes that log messages when disposed
  • Property accumulation - Build structured log data across nested scopes
  • Strict LIFO enforcement - Safety warnings for out-of-order scope disposal
  • Exception handling - Attach exceptions and modify messages before logging
  • Dynamic log levels - Change log level and message content before disposal

Installation

dotnet add package DevElf.Logging

Quick Start

Setup Dependency Injection

using DevElf.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

var services = new ServiceCollection()
    .AddLogging(builder => builder.AddConsole())
    .AddLogMessageScopes(); // Register log message scope services

var provider = services.BuildServiceProvider();
var logger = provider.GetRequiredService<ILogger<MyService>>();

Basic Usage

using var scope = logger.BeginMessageScope(LogLevel.Information, "Processing user request");
scope.SetProperty("UserId", userId);
scope.SetProperty("RequestId", requestId);

// Do work...

// Message is logged on dispose with all accumulated properties

Advanced Usage with Exception Handling

using var scope = logger.BeginMessageScope(LogLevel.Information, new EventId(1001, "DataImport"), "Starting data import");
scope.SetProperty("FileName", fileName);
scope.SetProperty("CorrelationId", correlationId);

try
{
    // Perform import operation
    var recordCount = await ImportDataAsync(fileName);
    scope.SetProperty("RecordCount", recordCount);
}
catch (Exception ex)
{
    scope.SetException(ex, setProperty: true); // Adds exception as property and sets Exception
    scope.ChangeLogLevel(LogLevel.Error);      // Change from Information to Error
    scope.ChangeMessage("Data import failed"); // Update the message
    throw;
}
// Logs either success or failure message with all properties

Nested Scopes

using var outerScope = logger.BeginMessageScope(LogLevel.Information, "Processing batch");
outerScope.SetProperty("BatchId", batchId);
outerScope.SetProperty("Environment", "Production");

foreach (var item in batch)
{
    using var innerScope = logger.BeginMessageScope(LogLevel.Debug, "Processing item");
    innerScope.SetProperty("ItemId", item.Id);
    innerScope.SetProperty("Environment", "Staging"); // Overrides parent value
    
    // Process item...
    
    // Inner scope logs with: BatchId, ItemId, Environment="Staging"
}

// Outer scope logs with: BatchId, Environment="Production"

API Reference

ILogMessageScope

Method Description
SetProperty(string key, object? value) Add or update a property in the scope
SetException(Exception exception, bool setProperty = false) Attach an exception to log on dispose
ChangeLogLevel(LogLevel logLevel) Change the log level for the final message
ChangeMessage(string message) Change the message text to log on dispose
ChangeEventId(EventId eventId) Change the event ID for the final message

Logger Extensions

Method Description
BeginMessageScope(LogLevel, string) Create a basic message scope
BeginMessageScope(LogLevel, EventId, string) Create a message scope with event ID

Important Notes

LIFO Disposal Required: Log message scopes must be disposed in Last-In-First-Out (LIFO) order. Disposing out of order logs a warning and the disposal is ignored, allowing a later correct disposal to succeed.

Property Inheritance: Properties from outer scopes are inherited by inner scopes. When property keys conflict, the innermost scope value takes precedence.

Requirements

  • .NET 10.0+
  • Microsoft.Extensions.Logging.Abstractions

Dependencies

  • DevElf - Core argument validation utilities

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.4.3-alpha.0 155 10/6/2025
0.4.0-alpha.7 159 9/30/2025
0.4.0-alpha.6 155 9/30/2025
0.3.5-alpha.5 124 9/28/2025