c# - 如何在 EF Core 3 中启用日志记录?
问题描述
我正在使用 Entity Framework Core 3 Preview 5 和 ASP.NET Core 3 Preview 5。在 Visual Studio 2019 的调试输出窗口中,我没有收到来自 EF Core 的日志。我阅读了文档,但之后我更加困惑:
使用“AddDbContext”方法之一时无需调用此方法。'AddDbContext' 将确保 EF 使用的 ILoggerFactory 是从应用程序服务提供者处获得的。
不过,这不是我的经验。
- 我尝试通过注入 to 来启用日志记录
ILoggerFactory
(ConfigureServices
我打算然后将其传递给DbContextOptionsBuilder.UseLoggerFactory
,但这已经不可能了,请参阅https://github.com/aspnet/Announcements/issues/353
那么,如何将日志记录设置到 EF Core 3.0 中的调试输出窗口?谢谢!
解决方案
3.0 RTM 及更高版本的更新:日志级别恢复为信息。检查过滤文档中记录的内容以获取更多详细信息
票数接近可能是因为问题中没有可以重现问题的代码。
在任何情况下,EF Core 都会在调试级别记录。通用主机构建器或 Web 主机构建器使用的默认级别是Information
. 日志级别必须更改为Trace
或Debug
。
默认情况下,此代码不会记录任何 EF 事件:
static async Task Main(string[] args)
{
var host = Host
.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
var configuration = context.Configuration;
services.AddDbContext<MyContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("someConnection")));
})
.Build();
using(var ctx=host.Services.GetRequiredService<MyContext>())
{
var cnt=await ctx.Customers.CountAsync();
Console.WriteLine(cnt);
}
}
它只会记录这个事件:
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 3.0.0-preview6.19304.10 initialized 'ConsolidatorsContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
要记录 EF 事件,我们需要将 EF Core 事件的日志记录级别更改为Trace
或Debug
通过appsettings.json
或代码。例如,将其包括在appsettings.json
:
"Logging": {
"LogLevel": {
"Microsoft.EntityFrameworkCore":"Debug"
}
},
将记录 EF 事件:
dbug: Microsoft.EntityFrameworkCore.Infrastructure[10401]
An 'IServiceProvider' was created for internal use by Entity Framework.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 3.0.0-preview6.19304.10 initialized 'MyContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
Opening connection to database 'Customers' on server '10.0.0.216'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
Opened connection to database 'Customers' on server '10.0.0.216'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT COUNT(*)
FROM [Customers] AS [c]
dbug: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (42ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT COUNT(*)
FROM [Customers] AS [c]
4
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20002]
Closing connection to database 'Customers' on server '10.0.0.216'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20003]
Closed connection to database 'Customers' on server '10.0.0.216'.
dbug: Microsoft.EntityFrameworkCore.Infrastructure[10407]
'MyContext' disposed.
推荐阅读
- python - 如果使用不平衡数据技术,是否可以处理一组词向量?
- c++ - 参数化自定义流操纵器 - 为什么重载“operator<<”?
- javascript - Java Script 随机数生成器问题
- oracle - 尝试使用私有端点访问我的 ADB 的数据库操作时出现 404
- javascript - 试图将属性重新分配到今天的日期
- python - 如何防止断管的多处理错误?
- c# - 在 WPF 中替代 300,000 个 DrawLine/DrawRectangle 调用,WriteableBitmap 不是一个选项
- flutter - 如何使嵌套的滚动视图不可滚动
- python - Numba Cuda 中的内置向量类型
- php - 在插入 mysql 之前将一个撇号替换为另一个撇号