1

using latest .Net Core (3.0 preview) and EF Core (3.0 preview) looking at a few online sources I did this:

Program.cs

public class MainWorker : IHostedService {

  public static readonly ILoggerFactory ConsoleLoggerFactory =
     LoggerFactory.Create(builder => builder.AddConsole();

MyDbContext.cs

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
  string connectionString = ...
    optionsBuilder
      .UseLoggerFactory(MainWorker.ConsoleLoggerFactory)
      .EnableSensitiveDataLogging(true)
      .UseSqlite(connectionString);
    }

This all builds and runs fine but when using EF to query my DB instead of the actual SQL queries I only see are entries such as:

info: Microsoft.EntityFrameworkCore.Infrastructure[10403] Entity Framework Core 3.0.0-preview4.19176.6 initialized 'MyDbContext' using provider 'Microsoft.EntityFrameworkCore.Sqlite' with options: SensitiveDataLoggingEnabled

How can I get the actual SQL query for an IQueryable ?

Any help ?

0xced
  • 25,219
  • 10
  • 103
  • 255
kofifus
  • 17,260
  • 17
  • 99
  • 173
  • Haven't tried it, but looks like you'd only expect to get logs for warnings and above. Are you sure your code will be generating any EF warnings/errors? – Ben May 06 '19 at 02:07
  • I'm not, got it from - https://stackoverflow.com/questions/53690820/how-to-create-a-loggerfactory-with-a-consoleloggerprovider - I'm not sure what `AddFilter` does, can't find docs – kofifus May 06 '19 at 03:18
  • ok I updated my question after removing AddFilter .. can you please have a look ? – kofifus May 06 '19 at 03:42

1 Answers1

4

Update: Based on the provided feedback, the below change has been reverted in 3.0 preview 7.


This is one of the expected breaking changes in EF Core 3.0 - Query execution is logged at Debug level.

The link explains why is that and how to get back the previous behavior of logging SQL at Info level (the default) using the DbContextOptionsBuilder:

.ConfigureWarnings(c => c.Log((RelationalEventId.CommandExecuting, LogLevel.Info))); 

Another way is to change the minimum log level for DbLoggerCategory.Database.Command category to Debug using the AddFilter method of the ILoggingBuilder:

.AddFilter(DbLoggerCategory.Database.Command.Name, LogLevel.Debug)
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • One should note that this breaking change will get reverted again: https://github.com/aspnet/EntityFrameworkCore/issues/14523 – stefan.at.kotlin Jun 18 '19 at 15:17
  • @stefan.at.wpf Well, it might or might not - one of the reasons I don't like preview (beta) versions. We'll see, for now we should rely on "official" information from EF Core documentation. – Ivan Stoev Jun 19 '19 at 08:19