Unable to resolve ILogger from Microsoft.Extensions.Logging

前端 未结 7 1029
太阳男子
太阳男子 2021-02-02 04:56

I\'ve configured my console application\'s Main like so

var services = new ServiceCollection()
                .AddLogging(logging => logging.Add         


        
7条回答
  •  迷失自我
    2021-02-02 05:43

    In .NET Core, ILogger is automatically registered for you. As ILogger inherits from ILogger, you can request an instance of ILogger from the IServiceProvider.

    For example: services.AddSingleton(svc => svc.GetRequiredService>());

    Note that this will return an ILogger wherever you've got a non-generic ILogger constructor parameter so if you need more than one, look into creating it specifically for that instance by using the AddSingleton (or transient/or scoped) implementationFactory override.

提交回复
热议问题