Class LoggerExtensions
- Namespace
- Codebelt.Extensions.Xunit.Hosting
- Assembly
- Codebelt.Extensions.Xunit.Hosting.dll
Extension methods for the ILogger<TCategoryName> interface.
public static class LoggerExtensions
- Inheritance
-
LoggerExtensions
Examples
The following example retrieves the captured log entries from an ILogger<T> after xUnit test logging has been registered. Use GetTestStore() on an untyped ILogger to search by category name, or GetTestStore<T>() to retrieve entries for a specific typed logger category.
using Codebelt.Extensions.Xunit;
using Codebelt.Extensions.Xunit.Hosting;
using Microsoft.Extensions.Logging;
namespace HostFixtureTests;
public class LogInspector
{
public ITestStore<XunitTestLoggerEntry> GetEntries(ILogger logger)
{
return logger.GetTestStore();
}
public ITestStore<XunitTestLoggerEntry> GetEntries<T>(ILogger<T> logger)
{
return logger.GetTestStore<T>();
}
}
Methods
GetTestStore(ILogger, string)
Returns the associated ITestStore<T> that is provided when settings up services from AddXunitTestLogging(IServiceCollection, LogLevel) or related.
public static ITestStore<XunitTestLoggerEntry> GetTestStore(this ILogger logger, string categoryName = null)
Parameters
loggerILoggerThe ILogger<TCategoryName> from which to retrieve the ITestStore<T>.
categoryNamestringThe category name for messages produced by the
logger-or-nullfor messages produced by all loggers.
Returns
- ITestStore<XunitTestLoggerEntry>
Returns an implementation of ITestStore<T> with all logged entries expressed as XunitTestLoggerEntry.
Exceptions
- ArgumentNullException
loggercannot be null.- ArgumentException
loggerdoes not contain a test store.- KeyNotFoundException
loggerdoes not contain a test store for the specifiedcategoryName.
GetTestStore<T>(ILogger<T>)
Returns the associated ITestStore<T> that is provided when settings up services from AddXunitTestLogging(IServiceCollection, LogLevel) or related.
public static ITestStore<XunitTestLoggerEntry> GetTestStore<T>(this ILogger<T> logger)
Parameters
loggerILogger<T>The ILogger<TCategoryName> from which to retrieve the ITestStore<T>.
Returns
- ITestStore<XunitTestLoggerEntry>
Returns an implementation of ITestStore<T> with all logged entries expressed as XunitTestLoggerEntry.
Type Parameters
T
Exceptions
- ArgumentNullException
loggercannot be null.- ArgumentException
loggerdoes not contain a test store.