XUnitLoggerBuilderExtensions Class

Provides extension methods for ILoggingBuilder to add xUnit.net logging support.

public static class XUnitLoggerBuilderExtensions

Example

var testOutputHelper = TestContext.Current.TestOutputHelper;
using var host = new HostBuilder()
    .ConfigureLogging(builder =>
    {
        builder.AddXunit(testOutputHelper);
    })
    .Build();

var logger = host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("MyCategory");
logger.LogInformation("This message will appear in the test output");

Methods

NameDescription
AddXunit(ILoggingBuilder)Adds an xUnit.net logger provider to the logging builder.
AddXunit(ILoggingBuilder, XUnitLoggerOptions?)Adds an xUnit.net logger provider to the logging builder with the specified options.
AddXunit(ILoggingBuilder, ITestOutputHelper?)Adds an xUnit.net logger provider to the logging builder with the specified test output helper.
AddXunit(ILoggingBuilder, ITestOutputHelper?, XUnitLoggerOptions?)Adds an xUnit.net logger provider to the logging builder with the specified test output helper and options.

AddXunit overloads

AddXunit(ILoggingBuilder)

XUnitLoggerBuilderExtensions.cs

Adds an xUnit.net logger provider to the logging builder.

public static ILoggingBuilder AddXunit(this ILoggingBuilder builder)
Parameters
builder ILoggingBuilder
The ILoggingBuilder to add the provider to.
Returns

ILoggingBuilder The ILoggingBuilder so that additional calls can be chained.

Remarks

Calling this method more than once registers a single provider. The logger resolves the test output helper from TestContext.Current when it writes, so a second registration would only duplicate the output.

AddXunit(ILoggingBuilder, XUnitLoggerOptions?)

XUnitLoggerBuilderExtensions.cs

Adds an xUnit.net logger provider to the logging builder with the specified options.

public static ILoggingBuilder AddXunit(this ILoggingBuilder builder, XUnitLoggerOptions? options)
Parameters
builder ILoggingBuilder
The ILoggingBuilder to add the provider to.
options XUnitLoggerOptions?
The logger options.
Returns

ILoggingBuilder The ILoggingBuilder so that additional calls can be chained.

Remarks

Each call adds a provider, so calling this method several times with a different options writes the logs several times.

AddXunit(ILoggingBuilder, ITestOutputHelper?)

XUnitLoggerBuilderExtensions.cs

Adds an xUnit.net logger provider to the logging builder with the specified test output helper.

public static ILoggingBuilder AddXunit(this ILoggingBuilder builder, ITestOutputHelper? testOutputHelper)
Parameters
builder ILoggingBuilder
The ILoggingBuilder to add the provider to.
testOutputHelper ITestOutputHelper?
The xUnit.net test output helper.
Returns

ILoggingBuilder The ILoggingBuilder so that additional calls can be chained.

Remarks

Each call adds a provider, so calling this method several times with a different test output helper writes the logs several times.

AddXunit(ILoggingBuilder, ITestOutputHelper?, XUnitLoggerOptions?)

XUnitLoggerBuilderExtensions.cs

Adds an xUnit.net logger provider to the logging builder with the specified test output helper and options.

public static ILoggingBuilder AddXunit(this ILoggingBuilder builder, ITestOutputHelper? testOutputHelper, XUnitLoggerOptions? options)
Parameters
builder ILoggingBuilder
The ILoggingBuilder to add the provider to.
testOutputHelper ITestOutputHelper?
The xUnit.net test output helper.
options XUnitLoggerOptions?
The logger options.
Returns

ILoggingBuilder The ILoggingBuilder so that additional calls can be chained.

Remarks

Each call adds a provider, so calling this method several times with a different test output helper and options writes the logs several times.