gpt4 book ai didi

c# - 在 Entity Framework Core 中启用原始 SQL 日志记录

转载 作者:太空狗 更新时间:2023-10-29 18:34:43 27 4
gpt4 key购买 nike

如何启用 DbCommand 原始 SQL 查询的日志记录?

我已将以下代码添加到我的 Startup.cs 文件中,但没有看到来自 Entity Framework Core 的任何日志条目。

void ConfigureServices(IServiceCollection services)
{
services.AddLogging();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug(LogLevel.Debug);
}

我期待看到这样的东西:

Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilder...
SELECT [t].[Id], [t].[DateCreated], [t].[Name], [t].[UserName]
FROM [Trips] AS [t]

最佳答案

从 MVC Core 2 开始,记录 SQL 是默认行为。只需确保 appSettings json 文件中的日志记录级别正确即可。

"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}

关于c# - 在 Entity Framework Core 中启用原始 SQL 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39282951/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com