title: How to Use LoggerFactory and Microsoft.Extensions.Logging for .NET Core Console Logging With C#
body: <p>I've created a console application that uses a service layer.</p>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>public static void Main(string[] args)
  {
      // Create service collection
      var serviceCollection = new ServiceCollection();
      ConfigureServices(serviceCollection);
  
      // Create service provider
      var serviceProvider = serviceCollection.BuildServiceProvider();
  
      // Entry to run app
      serviceProvider.GetService&lt;App&gt;().Run().RunSynchronously();
  }
  
  private static void ConfigureServices(IServiceCollection serviceCollection)
  {
      // Configuration
      var configuration = new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddJsonFile("appsettings.json", false)
          .Build();
  
      serviceCollection.AddOptions();
      serviceCollection.Configure&lt;Settings&gt;(options =&gt;
      {
          //...
      });
  
      // Services
      serviceCollection.AddTransient&lt;IOneService, OneService&gt;();
      serviceCollection.AddTransient&lt;ISecondService, SecondService&gt;();
  
      // Repositories
      serviceCollection.AddTransient&lt;MyContext, MyContext&gt;();
      serviceCollection.AddTransient&lt;IOneRepository, OneRepository&gt;();
  
      // App
      serviceCollection.AddTransient&lt;App&gt;();
  
      // Logger
  
      // Automapper
      serviceCollection.AddSingleton(new AutoMapperProfileConfiguration());
      serviceCollection.AddScoped&lt;IMapper&gt;(sp =&gt;
          new Mapper(sp.GetRequiredService&lt;IConfigurationProvider&gt;(), sp.GetService));
  }
  </code></pre>
  
  <p>I'm getting this error <code>System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger</code>1` and I'm guessing that i have to setup LoggerFactory and Microsoft.Extensions.Logging for .NET Core Logging, but I can't get it right.</p>
  
  <p>I've tried something like this in Main():</p>
  
  <pre><code>// Attempt 1
  ILoggerFactory loggerFactory = new LoggerFactory()
      .AddConsole()
      .AddDebug();
  ILogger logger = loggerFactory.CreateLogger&lt;Program&gt;();
  logger.LogInformation(
    "This is a test of the emergency broadcast system.");
  
  // Attempt 2
  serviceCollection.AddSingleton(new LoggerFactory()
      .AddConsole()
      .AddDebug());
  </code></pre>
  
  <p>Any ideas?</p>