title: How to configure logging for SQL for EFCore 2 in ASP.NET Core 2.0
body: <p>I know this sounds like an insanely stupid question, but I'm stuck.</p>
  
  <p>In the <a href="https://docs.microsoft.com/en-us/ef/core/miscellaneous/logging" rel="nofollow noreferrer"><code>Entity Framework &gt; Entity Framework Core &gt; Misc &gt; Logging</code></a> page (which incidentally hasn't been updated since Nov. 2016) it tells you to do the following:</p>
  
  <pre><code>protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      =&gt; optionsBuilder
          .UseLoggerFactory(MyLoggerFactory) // Warning: Do not create a new ILoggerFactory instance each time
          .UseSqlServer(
              @"Server=(localdb)\mssqllocaldb;Database=EFLogging;Trusted_Connection=True;ConnectRetryCount=0");
  </code></pre>
  
  <p>Sure enough if I do the following (adapting the above to the <code>ConfigureServices</code> method) I do get SQL output to the console / debug console when each command runs:</p>
  
  <pre><code>services.AddDbContext&lt;MyStoreContext&gt;(options =&gt;
  {
      options.UseSqlServer(connection);
      options.UseLoggerFactory(new LoggerFactory().AddConsole().AddDebug());
  });
  </code></pre>
  
  <p>However the documentation for <a href="https://github.com/aspnet/EntityFrameworkCore/blob/53c365a3f7c6ade7e4701b32b0f3943ea24a5b27/src/EFCore/DbContextOptionsBuilder.cs" rel="nofollow noreferrer"><code>UseLoggerFactory</code></a> extension method clearly says:</p>
  
  <blockquote>
    <p>Sets the ILoggerFactory that will be used to create ILoggerFactory instances
    for logging done by this context. It is never necessary to call this method 
    since EF can obtain or create a logger factory automatically.</p>
    
    <p>There is no need to call this method when using one of the
    'AddDbContext' methods.'AddDbContext' will ensure that the
    Microsoft.Extensions.Logging.ILoggerFactory used by EF is obtained
    from the application service provider.</p>
  </blockquote>
  
  <p>So it seems I'm not supposed to use it, and therefore I would assume it would just automatically use whatever I configure here:</p>
  
  <pre><code>services.AddLogging(options =&gt;
  {
      options.AddDebug();
      options.AddConsole();
  }
  </code></pre>
  
  <p>However it doesn't. This outputs nothing to the console if I only configure logging 'globally' here.</p>
  
  <p>What is the correct way to do logging in EFCore 2 under ASP.NET Core 2? </p>