MELT 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MELT --version 0.2.0                
NuGet\Install-Package MELT -Version 0.2.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="MELT" Version="0.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MELT --version 0.2.0                
#r "nuget: MELT, 0.2.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 MELT as a Cake Addin
#addin nuget:?package=MELT&version=0.2.0

// Install MELT as a Cake Tool
#tool nuget:?package=MELT&version=0.2.0                

Testing Library for Microsoft Extensions Logging.

Build Status alternate text is missing from this package README image

About MELT

MELT is a free, open source, testing library for the .NET Standard Microsoft Extensions Logging library. It is a solution to easily test logs.

It is a repackaging with a sweetened API and some omissions of Microsoft.Extensions.Logging.Testing, a library used internally in ASP.NET Core for testing the logging, given that there is currently no plan to offer an official package for it.

It is licensed under Apache License 2.0. Most of the code is copyrighted by the .NET Foundation as mentioned in the files headers.

If you like this project please don't forget to star it on GitHub or let me know with a tweet.

Quick start

  • Install the NuGet package MELT

    <PackageReference Include="MELT" Version="0.1.0" />
    
  • Get a test logger factory

    var loggerFactory = MELTBuilder.CreateLoggerFactory();
    
  • Get a logger from the factory as usual, to pass ro your fixture.

    var logger = loggerFactory.CreateLogger<Sample>();
    
Assert log entries

The logger factory expose a property LogEntries that enumerates all the logs captured. Each entry expose all the relevant property for a log.

For example, to test with xUnit that a single log has been emitted and it had a specific message:

var log = Assert.Single(loggerFactory.LogEntries);
Assert.Equal("The answer is 42", log.Message);
Assert scopes

The logger factory expose a property Scopes that enumerates all the scopes captured.

For example, to test with xUnit that a single scope has been emitted and it had a specific message:

var scope = Assert.Single(loggerFactory.Scopes);
Assert.Equal("I'm in the GET scope", scope.Message);

There is also a property Scope in each log entry to have the scope captured with that entry.

Easily test log or scope properties with xUnit
  • Install the NuGet package MELT.Xunit

    <PackageReference Include="MELT.Xunit" Version="0.1.0" />
    
  • Use the LogValuesAssert.Contains(...) helpers. For example, to test that a single log has been emitted and it had a property number with value 42:

    var log = Assert.Single(loggerFactory.LogEntries);
    LogValuesAssert.Contains("number", 42, log.Properties);
    
Full example

See SampleTest.

Quick start for ASP.NET Core integration tests

  • Install the NuGet package MELT

    <PackageReference Include="MELT" Version="0.1.0" />
    
  • Create a test sink using MELTBuilder.CreateTestSink(...), where you can also customize the bahaviour.

    For example to filter all log entries and scopes not generated by loggers consumed in the SampleWebApplication.* namespace (this filters the logger name so it assumes you are using ILogger<T> or following the default naming convention for your loggers.)

    ITestSink _sink = MELTBuilder.CreateTestSink(options => options.FilterByNamespace(nameof(SampleWebApplication)));
    

    You can also filter by logger name using FilterByTypeName<T>() or FilterByLoggerName(string name).

  • Use the AddTestLogger(...) extension method to add the test logger provider to the logging builder. This can be done where you are already configuring the web host builder.

    Configure the logger using WithWebHostBuilder on the factory.

    using using Microsoft.Extensions.Logging;
    // ...
    factory = factory.WithWebHostBuilder(builder => builder.ConfigureLogging(logging => logging.AddTestLogger(_sink)));
    

    Alternatively, you can configure the logger builder in the ConfigureWebHost implementation of your custom WebApplicationFactory<T>. If you chose so, the same sink will be used by all tests using the same factory. You can clear the sink in the test costrctuctor with Clear() if you like to have a clean state before each test, as xUnit will not run tests consuming the same fixture in parallel.

    The logger will be automatically injected with Dependency Injection.

Assert log entries

The sink expose a property LogEntries that enumerates all the logs captured. Each entry expose all the relevant property for a log.

For example, to test with xUnit that a single log has been emitted and it had a specific message:

var log = Assert.Single(sink.LogEntries);
Assert.Equal("The answer is 42", log.Message);
Assert scopes

The sink expose a property Scopes that enumerates all the scopes captured.

For example, to test with xUnit that a single scope has been emitted and it had a specific message:

var scope = Assert.Single(sink.Scopes);
Assert.Equal("I'm in the GET scope", scope.Message);

There is also a property Scope in each log entry to have the scope captured with that entry.

Easily test log or scope properties with xUnit
  • Install the NuGet package MELT.Xunit

    <PackageReference Include="MELT.Xunit" Version="0.1.0" />
    
  • Use the LogValuesAssert.Contains(...) helpers. For example, to test that a single log has been emitted and it had a property number with value 42:

    var log = Assert.Single(sink.LogEntries);
    LogValuesAssert.Contains("number", 42, log.Properties);
    
Full example

See LoggingTest.

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

Showing the top 3 NuGet packages that depend on MELT:

Package Downloads
MELT.Xunit

xUnit helper for the MELT testing library to verify that the correct logs are generated by apps or libraries using the .NET Standard Microsoft.Extensions.Logging library.

MELT.AspNetCore

Helper for the MELT testing library to verify that the correct logs are generated by ASP.NET Core applications, when writing integration tests with Microsoft.AspNetCore.Mvc.Testing.

MELT.Serilog

Testing library to verify that the correct logs are generated by apps or libraries using the .NET Standard Microsoft.Extensions.Logging library with Serilog as provider.

GitHub repositories (4)

Showing the top 4 popular GitHub repositories that depend on MELT:

Repository Stars
quartznet/quartznet
Quartz Enterprise Scheduler .NET
dotnet/aspnet-api-versioning
Provides a set of libraries which add service API versioning to ASP.NET Web API, OData with ASP.NET Web API, and ASP.NET Core.
Mongo2Go/Mongo2Go
Mongo2Go - MongoDB for .NET integration tests
imazen/imageflow-server
A super-fast image server to speed up your site - deploy as a microservice, serverless, or embeddable.
Version Downloads Last updated
0.9.0 474,288 8/6/2023
0.8.0 828,826 4/26/2021
0.7.0 88,234 2/21/2021
0.6.0 48,147 11/7/2020
0.6.0-preview.1 244 10/16/2020
0.5.3 98,157 8/31/2020
0.5.2 2,056 8/30/2020
0.5.1 1,968 8/30/2020
0.5.0 3,182 8/24/2020
0.4.0 25,531 2/18/2020
0.3.0 21,120 11/24/2019
0.2.0 1,345 11/3/2019
0.1.0 14,151 10/26/2019