XUnitLoggerBuilderExtensions Class
Provides extension methods for ILoggingBuilder to add xUnit.net logging support.
public static class XUnitLoggerBuilderExtensionsExample
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
| Name | Description |
|---|---|
| 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.csAdds an xUnit.net logger provider to the logging builder.
public static ILoggingBuilder AddXunit(this ILoggingBuilder builder)Parameters
builderILoggingBuilder- The
ILoggingBuilderto 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.csAdds an xUnit.net logger provider to the logging builder with the specified options.
public static ILoggingBuilder AddXunit(this ILoggingBuilder builder, XUnitLoggerOptions? options)Parameters
builderILoggingBuilder- The
ILoggingBuilderto add the provider to. optionsXUnitLoggerOptions?- 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.csAdds 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
builderILoggingBuilder- The
ILoggingBuilderto add the provider to. testOutputHelperITestOutputHelper?- 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.csAdds 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
builderILoggingBuilder- The
ILoggingBuilderto add the provider to. testOutputHelperITestOutputHelper?- The xUnit.net test output helper.
optionsXUnitLoggerOptions?- 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.