Meziantou.Extensions.Logging.InMemory 1.2.1

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

// Install Meziantou.Extensions.Logging.InMemory as a Cake Tool
#tool nuget:?package=Meziantou.Extensions.Logging.InMemory&version=1.2.1                

Meziantou.Extensions.Logging.InMemory

using var loggerProvider = new InMemoryLoggerProvider();
var logger = loggerProvider.CreateLogger("MyLogger");

// do stuff with the logger

// Assert
Assert.Empty(loggerProvider.Logs.Errors);
Assert.Single(loggerProvider.Logs, log => log.Message.Contains("test") && log.EventId.Id == 1);

If you are using a WebApplicationFactory:

using var loggerProvider = new InMemoryLoggerProvider();
using var factory = new WebApplicationFactory<Program>()
    .WithWebHostBuilder(builder =>
    {
        builder.ConfigureLogging(builder =>
        {
            // You can override the logging configuration if needed
            //builder.SetMinimumLevel(LogLevel.Trace);
            //builder.AddFilter(_ => true);

            builder.Services.AddSingleton<ILoggerProvider>(loggerProvider);
        });
    });

Blog post about testing logging: How to test the logs from ILogger in .NET

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 is compatible. 
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 (1)

Showing the top 1 popular GitHub repositories that depend on Meziantou.Extensions.Logging.InMemory:

Repository Stars
mehdihadeli/vertical-slice-api-template
An asp.net core template based on .Net 8, Vertical Slice Architecture, CQRS, Minimal APIs, API Versioning and Swagger.
Version Downloads Last updated
1.2.1 310 11/17/2024
1.2.0 33,973 1/7/2024
1.1.0 509 1/7/2024
1.0.5 13,328 11/15/2023
1.0.2 4,646 4/10/2023
1.0.1 1,370 12/4/2021
1.0.0 353 9/6/2021