Xunit LoggerProvider for Microsoft.Extensions.Logging to write log events to the Xunit test output.
To use the provider, first install the NuGet package:
Install-Package Xunit.Extensions.Logging
Or
dotnet add package Xunit.Extensions.Logging
Then enable the Xunit output using AddXunit(ITestOutputHelper)
:
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;
public class SampleTest
{
private readonly ILogger logger;
public SampleTest(ITestOutputHelper outputHelper)
{
logger = LoggerFactory
.Create(builder =>
{
builder
.AddXunit(outputHelper);
// Add other loggers, e.g.: AddConsole, AddDebug, etc.
})
.CreateLogger<SampleTest>();
}
[Fact]
public void DoSomeTest()
{
// Arrange
// Act
// Assert
logger.LogInformation("Hello world!");
}
}
Log events will be printed to the Xunit test output:
Clone this repo:
git clone https://github.com/yorchideas/Xunit.Extensions.Logging.git
Change directory to repo root:
cd Xunit.Extensions.Logging
Build the solution:
dotnet restore
dotnet build
This will result in the following:
- Restore all NuGet packages required for building
- Build all projects. Final binaries are placed into
<repo_root>\build\bin\<Configuration>
To run unit tests, execute:
dotnet test ./test/Xunit.Extensions.Logging.UnitTests/Xunit.Extensions.Logging.UnitTests.csproj
This project is licensed under the MIT license.