extensions: Unable to resolve service for type 'Microsoft.Extensions.Logging.ILoggerFactory' in .net Core 3.0
Describe the bug
Got error Unable to resolve service for type ‘Microsoft.Extensions.Logging.ILoggerFactory’ when using public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
To Reproduce
Steps to reproduce the behavior:
- Using this version of ASP.NET Core ‘.Net Core SDK 3.0.100-preview2’ and VS 2019 Preview
- Create a new Asp.Net Core MVC Template with .net core 3.0
- Modify
Startup.cswith
public class Startup
{
private readonly ILogger<Startup> _logger;
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
{
Configuration = configuration;
_logger = loggerFactory.CreateLogger<Startup>();
}
- Run project and got error
An error occurred while starting the application. InvalidOperationException: Unable to resolve service for type ‘Microsoft.Extensions.Logging.ILoggerFactory’ while attempting to activate ‘WebApplication21.Startup’. Microsoft.Extensions.DependencyInjection.ActivatorUtilities+ConstructorMatcher.CreateInstance(IServiceProvider provider)
InvalidOperationException: Unable to resolve service for type ‘Microsoft.Extensions.Logging.ILoggerFactory’ while attempting to activate ‘WebApplication21.Startup’. Microsoft.Extensions.DependencyInjection.ActivatorUtilities+ConstructorMatcher.CreateInstance(IServiceProvider provider) Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, object[] parameters) Microsoft.AspNetCore.Hosting.Internal.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services) Microsoft.AspNetCore.Hosting.Internal.GenericWebHostBuilder+<>c__DisplayClass13_0.<UseStartup>b__2(IApplicationBuilder app) Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter+<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app) Microsoft.AspNetCore.HostFilteringStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app) Microsoft.AspNetCore.Hosting.Internal.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
Expected behavior
Successfully resove service ILoggerFactory loggerFactory
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 13
- Comments: 15 (3 by maintainers)
Yes, in 3.0 we made a change to limit the types you can inject in to
Startup. It was a misleading feature because it used a completely separate DI container to the rest of the app. You can still injectILogger<T>in to yourConfiguremethod directly, but no longer in to the constructor orConfigureServices. (cc @Tratcher)See https://github.com/aspnet/Announcements/issues/353 for more details and https://github.com/aspnet/AspNetCore/issues/9337 for discussions on this topic.
Those aren’t our docs but I submitted a PR to update it: https://github.com/MicrosoftDocs/azure-docs/pull/33059 . Feel free to suggest edits for any other docs that are inconsistent here!
What about updating the docs to reflect this will break in future releases?
https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#capturing-ilogger-logs-from-startupcs-programcs-in-aspnet-core-applications
I’ve just stuck with BackgroundService’s because No service for type ‘Microsoft.Extensions.Logging.ILogger’ has been registered. Not typed ILogger! So much time spent for nothing ((