Maris.Logging.Testing
1.0.0
Prefix Reserved
dotnet add package Maris.Logging.Testing --version 1.0.0
NuGet\Install-Package Maris.Logging.Testing -Version 1.0.0
<PackageReference Include="Maris.Logging.Testing" Version="1.0.0" />
<PackageVersion Include="Maris.Logging.Testing" Version="1.0.0" />
<PackageReference Include="Maris.Logging.Testing" />
paket add Maris.Logging.Testing --version 1.0.0
#r "nuget: Maris.Logging.Testing, 1.0.0"
#addin nuget:?package=Maris.Logging.Testing&version=1.0.0
#tool nuget:?package=Maris.Logging.Testing&version=1.0.0
Maris.Logging.Testing
This library provides Microsoft.Extensions.Logging.ILogger
and Microsoft.Extensions.Logging.ILogger<TCategoryName>
instances which can be used in xUnit tests.
You can check the log output from test classes on Visual Studio Test Explorer.
This library also provides the functionality to verify the log output within the test code by integrating Microsoft.Extensions.Logging.Testing.FakeLogger
.
Install
Run the following command in Package Manager Console or Command Prompt to install Maris.Logging.Testing
.
- Package Manager Console
Install-Package Maris.Logging.Testing
- Command Prompt
dotnet add package Maris.Logging.Testing
Usage
Suppose you have a class that requires an ILogger<TCategoryName>
instance as shown below, and need to write xUnit test code for this class.
using Microsoft.Extensions.Logging;
namespace Maris;
public class TestTarget
{
private readonly ILogger<TestTarget> Logger;
public TestTarget(ILogger<TestTarget> logger)
=> this.Logger = logger;
// ...
}
Follow these steps to use a test logger in the test code.
- Install template for xUnit v3, and run
dotnet new xunit3
to create a unit test project. - Install
Maris.Logging.Testing
in the unit test project. - In the test class, define a constructor with a
Xunit.ITestOutputHelper
parameter. - Create a
Maris.Logging.Testing.Xunit.TestLoggerManager
instance in the constructor, and store it in a field. Pass anITestOutputHelper
instance to the constructor ofTestLoggerManager
. - Call
CreateLogger
method ofTestLoggerManager
in the test method to create anILogger
orILogger<TCategoryName>
instance which can be passed to the test class.
using Maris.Logging.Testing.Xunit;
using Xunit;
namespace Maris.Tests;
public class TestClass1
{
private readonly TestLoggerManager loggerManager;
public TestClass1(ITestOutputHelper testOutputHelper)
=> this.loggerManager = new TestLoggerManager(testOutputHelper);
[Fact]
public void TestMethod()
{
// Creates ILogger<T> instance
var logger = this.loggerManager.CreateLogger<TestTarget>();
var target = new TestTarget(logger);
// Do something...
// TestLoggerManager.LogCollector exposes FakeLogger.Collector
Assert.Equal(1, this.loggerManager.LogCollector.Count);
Assert.Equal("expectedCategory", this.loggerManager.LogCollector.LatestRecord.Category);
Assert.Equal("expectedMessage", this.loggerManager.LogCollector.LatestRecord.Message);
}
}
When testing Microsoft.Extensions.Hosting.IHost
, AddTestLogging
method can be used to register a test logger in the DI container.
AddTestLogging
method is defined in Microsoft.Extensions.DependencyInjection.TestLoggerServiceCollectionExtensions
class.
Here is an example of xUnit test code.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Maris.Logging.Testing.Xunit;
using Xunit;
namespace Maris.Tests;
public class TestClass2
{
private readonly TestLoggerManager loggerManager;
public TestClass2(ITestOutputHelper testOutputHelper)
=> this.loggerManager = new TestLoggerManager(testOutputHelper);
[Fact]
public async Task TestMethod()
{
using var app = this.CreateHost();
var cancellationToken = TestContext.Current.CancellationToken;
await app.RunAsync(cancellationToken);
// Do something...
}
private IHost CreateHost()
{
var builder = Host.CreateDefaultBuilder();
builder.ConfigureServices((context, services) =>
{
// Register a test logger
services.AddTestLogging(this.loggerManager);
// Do something...
});
return builder.Build();
}
}
The log output during the test execution is displayed in Test Explorer as follows.
For more details, see Integration tests in ASP.NET Core.
Dependencies
-
The package used for developing xUnit v3 extensions.
Maris.Logging.Testing
only supports the unit test project with xUnit v3. Microsoft.Extensions.Diagnostics.Testing
The library for testing log output.
License
Product | Versions 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 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Diagnostics.Testing (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- xunit.v3.extensibility.core (>= 2.0.0)
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 |
---|---|---|
1.0.0 | 428 | 3/31/2025 |
1.0.0-Beta5 | 130 | 3/31/2025 |
1.0.0-Beta4 | 131 | 3/31/2025 |
1.0.0-Beta3 | 444 | 3/26/2025 |
1.0.0-Beta2 | 440 | 3/26/2025 |