Skip to content

[API Proposal]: FakeLoggerProvider - Create FakeLogger<T> #6229

@codin-dev

Description

@codin-dev

Background and motivation

FakeLoggerProvider currently allows the creation of non-generic loggers using:

var logger = loggerProvider.CreateLogger("CustomLogger");

However, it does not support creating generic loggers of type FakeLogger<T>, which can be useful when testing logging behavior in a type-safe manner. By allowing generic loggers, we can ensure that FakeLogger instances share a common FakeLogCollector, as well as a common IExternalScopeProvider, thereby providing a consistent log collection and scope management experience across generic and non-generic loggers.

Adding support for CreateLogger<T>() will improve usability and consistency in unit tests and middleware scenarios where logging is commonly scoped to specific types.

API Proposal

New Method in FakeLoggerProvider

public FakeLogger<T> CreateLogger<T>();

Signature Details:

  • T: The type for which the logger is created.
  • Returns: An instance of FakeLogger<T> that utilizes the shared FakeLogCollector and IExternalScopeProvider.

API Usage

var loggerProvider = new FakeLoggerProvider();
var logger = loggerProvider.CreateLogger<MyMiddleware>();

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions