dotnet / extensions

This repository contains a suite of libraries that provide facilities commonly needed when creating production-ready applications.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[API Proposal]: Add FakeLoggerFactory implementing ILoggerFactory

MichaelPeter opened this issue · comments

Background and motivation

When creating a mock Service Host for aspnetcore, I want to track the logging, but for that I need a ILoggerFactory, to by used by aspnetcore.

The Microsoft.Extensions.Logging.Testing namespace only provides a FakeLoggerProvider.

If you don't add these could you maybe explain why? Is there already a shell class?

API Proposal

using Microsoft.Extensions.Logging

namespace Microsoft.Extensions.Logging.Testing;

public class FakeLoggerFactory : ILoggerFactory
{
    // A extension method provides the geneig implementation
    public void CreateLogger() { }

    public void AddProvider(ILoggerProvider provider) {}

    public FakeLogCollector Collector {get }
}

API Usage

using FluentAssertions;

var loggerFactory = new FakeLoggerFactory();

IServiceCollection sc = new ServiceCollection();
sc.AddSingleton<ILoggerFactory>(loggerFactory);

...

loggerFactory.Collector.GetSnapshot().
Where(logEntry => logEntry.Level = LogLevel.Error)
.Should().Be(1, because: "A error should have happened.");

Alternative Designs

No response

Risks

No response

Sorry my Mistake, I can just create a LoggerFactory, I don't need a specifc FakeLoggerFactory.