适用于SQLite的.NET Core 3.0 ConsoleLoggerFactory [英] .NET Core 3.0 ConsoleLoggerFactory for SQLite

查看:87
本文介绍了适用于SQLite的.NET Core 3.0 ConsoleLoggerFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用最新的.Net Core(3.0预览版)和EF Core(3.0预览版)查看一些在线资源,我这样做:

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);
    }

所有这些都构建并运行良好,但是当使用EF查询数据库而不是仅 的实际SQL查询时,看到的条目如下:

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:

信息:Microsoft.EntityFrameworkCore.Infrastructure [10403]实体框架核心3.0.0-preview4.19176.6使用提供程序初始化了"MyDbContext"带有选项的"Microsoft.EntityFrameworkCore.Sqlite":SensitiveDataLoggingEnabled

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

如何获取IQueryable的实际SQL查询?

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

有帮助吗?

推荐答案

更新:根据提供的反馈,以下更改为

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

这是预期的重大更改-查询执行记录在调试级.

该链接说明了为什么原因以及如何使用 DbContextOptionsBuilder 恢复在 Info 级别(默认)记录SQL的先前行为:

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))); 

另一种方法是使用 AddFilter 方法将 DbLoggerCategory.Database.Command 类别的最低日志级别更改为 Debug > ILoggingBuilder :

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)

这篇关于适用于SQLite的.NET Core 3.0 ConsoleLoggerFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆