问题
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 ?
回答1:
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)
来源:https://stackoverflow.com/questions/55997906/net-core-3-0-consoleloggerfactory-for-sqlite