.NET Core 3.0 ConsoleLoggerFactory for SQLite

本小妞迷上赌 提交于 2019-12-11 07:46:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!