Skip to content

Consider adding a XUnitLogger<T> #856

@paulomorgado

Description

@paulomorgado

Is your feature request related to a problem?

In unit tests, there are times when I need an ILogger<T> without the overhead of dependency injection, ILoggerFactory, or a LoggerProvider. This can simplify test setup and make the tests more straightforward.

Describe the solution you'd like

I would like to have a XUnitLogger<T> class that implements the ILogger<T> interface. This logger should be easy to instantiate directly in unit tests without requiring dependency injection, ILoggerFactory, or a provider. It should integrate with xUnit's output capturing mechanisms to display log messages in the test results and support various log levels.

Describe alternatives you've considered

I haven't considered any alternatives that would be as simple and lightweight as this.

Simple implementation

    private sealed class XUnitLogger<T> : XUnitLogger, ILogger<T>
    {
        public XUnitLogger(IMessageSink messageSink, XUnitLoggerOptions options) : base(typeof(T).FullName, messageSink, options)
        {
        }

        public XUnitLogger(IMessageSinkAccessor accessor, XUnitLoggerOptions options) : base(typeof(T).FullName, accessor, options)
        {
        }

        public XUnitLogger(ITestOutputHelper outputHelper, XUnitLoggerOptions options) : base(typeof(T).FullName, outputHelper, options)
        {
        }

        public XUnitLogger(ITestOutputHelperAccessor accessor, XUnitLoggerOptions options) : base(typeof(T).FullName, accessor, options)
        {
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestA request for new functionality

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions