Skip to content

InfrastructureExtensions GetService throws an error. #30504

@qsdfplkj

Description

@qsdfplkj

I use: _logger = this.GetService<ILogger<MyContext>>(); inside the constructor of MyContext

The call to InfrastructureExtensions.GetService however throws an error in case there is no logger registered.

I wasn't expecting this as i assumed it would behave like the regular serviceprovider. getservice.
as compared with GetRequiredService that could throw an error.

Is there any possibility to get an instance of the logger without getting the error in case it's not configured?
(I know i can add a parameter to the constructor but that isn't an option for some reason.) or maybe update the InfrastructureExtensions to support both getservice and getrequiredservice?

As a workaround I now use: new Logger<MyContext>(this.GetService<ILoggerFactory>());

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions