question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to resolve service for type 'Microsoft.Extensions.Logging.ILoggerFactory' in .net Core 3.0

See original GitHub issue

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:

  1. Using this version of ASP.NET Core ‘.Net Core SDK 3.0.100-preview2’ and VS 2019 Preview
  2. Create a new Asp.Net Core MVC Template with .net core 3.0
  3. Modify Startup.cs with
public class Startup
    {
        private readonly ILogger<Startup> _logger;
        public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
        {
            Configuration = configuration;
            _logger = loggerFactory.CreateLogger<Startup>();
        }
  1. 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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:13
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
analogrelaycommented, May 5, 2019

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 inject ILogger<T> in to your Configure method directly, but no longer in to the constructor or ConfigureServices. (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.

2reactions
analogrelaycommented, Jun 11, 2019

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to resolve ILogger from Microsoft.Extensions.Logging
I am assuming you are using the default template for .net core web application. In your your Startup.cs you should have a method...
Read more >
Unable to resolve service for type microsoft.extensions ...
A runtime error is triggered when using the Microsoft ILogger ... Solved: Unable to resolve service for type microsoft.extensions.logging.
Read more >
Unable to resolve service for type 'Microsoft.Extensions. ...
I created an ASP.NET Core 3.0 Web Application with the default template in Visual Studio 2019 Preview 2.2 and tried to inject an...
Read more >
Unable to resolve service for type 'Microsoft.Extensions ...
Coding example for the question Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger`1[WebApplication1.Startup]'-.net-core.
Read more >
Logging in .NET Core and ASP.NET Core
Learn how to use the logging framework provided by the Microsoft.Extensions.Logging NuGet package.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found