Class ServiceCollectionExtensions
- Namespace
- Codebelt.Extensions.Xunit.Hosting
- Assembly
- Codebelt.Extensions.Xunit.Hosting.dll
Extension methods for the IServiceCollection interface.
public static class ServiceCollectionExtensions
- Inheritance
-
ServiceCollectionExtensions
Examples
The following example registers xUnit test logging and output helper services on an IServiceCollection so that ILogger<T> output is captured during a test run. Call AddXunitTestLogging with a minimum LogLevel, then register the ITestOutputHelperAccessor using AddXunitTestLoggingOutputHelperAccessor or a custom implementation with AddXunitTestLoggingOutputHelperAccessor<T>.
using Codebelt.Extensions.Xunit.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace HostFixtureTests;
public class ExampleFixture
{
public IServiceCollection ConfigureServices(IServiceCollection services)
{
services.AddXunitTestLogging(LogLevel.Information);
services.AddXunitTestLoggingOutputHelperAccessor();
return services;
}
}
Methods
AddXunitTestLogging(IServiceCollection, LogLevel)
Adds a unit test optimized implementation of logging to the services collection.
public static IServiceCollection AddXunitTestLogging(this IServiceCollection services, LogLevel minimumLevel = LogLevel.Trace)
Parameters
servicesIServiceCollectionThe IServiceCollection to extend.
minimumLevelLogLevelThe LogLevel that specifies the minimum level to include for the logging.
Returns
- IServiceCollection
A reference to
servicesso that additional configuration calls can be chained.
Exceptions
- ArgumentNullException
servicescannot be null.
AddXunitTestLogging(IServiceCollection, ITestOutputHelper, LogLevel)
Adds a unit test optimized implementation of output logging to the services collection.
public static IServiceCollection AddXunitTestLogging(this IServiceCollection services, ITestOutputHelper output, LogLevel minimumLevel = LogLevel.Trace)
Parameters
servicesIServiceCollectionThe IServiceCollection to extend.
outputITestOutputHelperThe Xunit.ITestOutputHelper that provides the output for the logging.
minimumLevelLogLevelThe LogLevel that specifies the minimum level to include for the logging.
Returns
- IServiceCollection
A reference to
servicesso that additional configuration calls can be chained.
Exceptions
- ArgumentNullException
servicescannot be null -or-outputcannot be null.
AddXunitTestLoggingOutputHelperAccessor(IServiceCollection)
Adds a default implementation of ITestOutputHelperAccessor to the services collection.
public static IServiceCollection AddXunitTestLoggingOutputHelperAccessor(this IServiceCollection services)
Parameters
servicesIServiceCollectionThe IServiceCollection to extend.
Returns
- IServiceCollection
A reference to
servicesso that additional configuration calls can be chained.
AddXunitTestLoggingOutputHelperAccessor<T>(IServiceCollection)
Adds a specified implementation of ITestOutputHelperAccessor to the services collection.
public static IServiceCollection AddXunitTestLoggingOutputHelperAccessor<T>(this IServiceCollection services) where T : class, ITestOutputHelperAccessor
Parameters
servicesIServiceCollectionThe IServiceCollection to extend.
Returns
- IServiceCollection
A reference to
servicesso that additional configuration calls can be chained.
Type Parameters
TThe type of the implementation of ITestOutputHelperAccessor.
Exceptions
- ArgumentNullException
servicescannot be null.