Table of Contents

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

logger ILogger

The ILogger<TCategoryName> from which to retrieve the ITestStore<T>.

categoryName string

The category name for messages produced by the logger -or- null for messages produced by all loggers.

Returns

ITestStore<XunitTestLoggerEntry>

Returns an implementation of ITestStore<T> with all logged entries expressed as XunitTestLoggerEntry.

Exceptions

ArgumentNullException

logger cannot be null.

ArgumentException

logger does not contain a test store.

KeyNotFoundException

logger does not contain a test store for the specified categoryName.

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

logger ILogger<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

logger cannot be null.

ArgumentException

logger does not contain a test store.