Nuzigor.Extensions.Logging.Memory 0.5.0

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

// Install Nuzigor.Extensions.Logging.Memory as a Cake Tool
#tool nuget:?package=Nuzigor.Extensions.Logging.Memory&version=0.5.0

Nuzigor.Extensions.Logging.Memory

In-memory implementation of Microsoft.Extensions.Logging.ILoggerProvider.

The purpose of this library is to capture and check log messages in tests.

Design principles

  • Full support of ISupportExternalScope
  • Capture both rendered messages and state properties
  • Use the same data structure for both log and scope states
  • Capture all scopes and their properties for each log entry
  • Expose OriginalFormat as an additional property

Typical usage

First:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using Nuzigor.Extensions.Logging.Memory;

And then in a test method:

var services = new ServiceCollection();

// Add memory logger provider
services.AddLogging(builder => builder.AddMemory());

var sp = services.BuildServiceProvider();

var loggerFactory = sp.GetRequiredService<ILoggerFactory>();

var logger = loggerFactory.CreateLogger("Category1");
using (var scope = logger.BeginScope("Scope Arg: {ScopeArg}", "ScopeValue"))
{
    logger.LogInformation("{Arg1}, {Arg2}", 15, "SomeText");
}

// Get the service to access captured logs
var memorySink = sp.GetRequiredService<IMemoryLoggerSink>();

Assert.That(memorySink.Logs, Has.Count.EqualTo(1));
var log = memorySink.Logs.ElementAt(0);

Assert.That(log.Message, Is.EqualTo("15, SomeText"));
Assert.That(log.EventId.Id, Is.EqualTo(0));
Assert.That(log.Message, Is.EqualTo("15, SomeText"));
Assert.That(log.State.Message, Is.EqualTo("15, SomeText"));
Assert.That(log.Category, Is.EqualTo("Category1"));
Assert.That(log.LogLevel, Is.EqualTo(LogLevel.Information));
Assert.That(log.Exception, Is.Null);
Assert.That(log.State.Properties, Has.Count.EqualTo(3));

var prop = log.State.Properties.ElementAt(0);
Assert.That(prop.Key, Is.EqualTo("Arg1"));
Assert.That(prop.Value, Is.EqualTo(15));

prop = log.State.Properties.ElementAt(1);
Assert.That(prop.Key, Is.EqualTo("Arg2"));
Assert.That(prop.Value, Is.EqualTo("SomeText"));

prop = log.State.Properties.ElementAt(2);
Assert.That(prop.Key, Is.EqualTo("{OriginalFormat}"));
Assert.That(prop.Value, Is.EqualTo("{Arg1}, {Arg2}"));

Assert.That(log.State.OriginalFormat, Is.EqualTo("{Arg1}, {Arg2}"));

Assert.That(log.Scopes, Has.Exactly(1).Items);

var logScope = log.Scopes.ElementAt(0);
Assert.That(logScope.Message, Is.EqualTo("Scope Arg: ScopeValue"));

Assert.That(logScope.Properties, Has.Exactly(2).Items);

var prop = logScope.Properties.ElementAt(0);
Assert.That(prop.Key, Is.EqualTo("ScopeArg"));
Assert.That(prop.Value, Is.EqualTo("ScopeValue"));

prop = logScope.Properties.ElementAt(1);
Assert.That(prop.Key, Is.EqualTo("{OriginalFormat}"));
Assert.That(prop.Value, Is.EqualTo("Scope Arg: {ScopeArg}"));

Assert.That(logScope.OriginalFormat, Is.EqualTo("Scope Arg: {ScopeArg}"));
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
0.5.0 416 5/2/2022
0.1.0 368 5/1/2022