如何启用 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/