.NET Core 3.0 用于 SQLite 的 ConsoleLoggerFactory
.NET Core 3.0 ConsoleLoggerFactory for SQLite
使用最新的 .Net Core(3.0 预览版)和 EF Core(3.0 预览版)查看一些在线资源,我这样做了:
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 查询时,我 仅 看到的条目如下:
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 查询?
有什么帮助吗?
更新: 根据提供的反馈,以下更改已 reverted in 3.0 preview 7。
这是预期的 breaking changes in EF Core 3.0 - Query execution is logged at Debug level。
link 解释了为什么会这样,以及如何使用 DbContextOptionsBuilder
恢复以前在 Info
级别(默认)记录 SQL 的行为:
.ConfigureWarnings(c => c.Log((RelationalEventId.CommandExecuting, LogLevel.Info)));
另一种方法是使用 ILoggingBuilder
:
的 AddFilter
方法将 DbLoggerCategory.Database.Command
类别的最低日志级别更改为 Debug
.AddFilter(DbLoggerCategory.Database.Command.Name, LogLevel.Debug)
使用最新的 .Net Core(3.0 预览版)和 EF Core(3.0 预览版)查看一些在线资源,我这样做了:
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 查询时,我 仅 看到的条目如下:
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 查询?
有什么帮助吗?
更新: 根据提供的反馈,以下更改已 reverted in 3.0 preview 7。
这是预期的 breaking changes in EF Core 3.0 - Query execution is logged at Debug level。
link 解释了为什么会这样,以及如何使用 DbContextOptionsBuilder
恢复以前在 Info
级别(默认)记录 SQL 的行为:
.ConfigureWarnings(c => c.Log((RelationalEventId.CommandExecuting, LogLevel.Info)));
另一种方法是使用 ILoggingBuilder
:
AddFilter
方法将 DbLoggerCategory.Database.Command
类别的最低日志级别更改为 Debug
.AddFilter(DbLoggerCategory.Database.Command.Name, LogLevel.Debug)