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

标签 c# entity-framework

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

相关文章:

c# - 如何在 C# 中访问类的属性

c# - AngularJS Controller 找不到 C# Controller

c# - 如何在 DbContext 类中编写扩展方法?

c# - 三层 Entity Framework 应用程序中的 DTO

c# - 如何为自定义队列实现 <IEnumerable>?

c# - 每次运行后清除与 tracelistener 关联的日志?

c# - 如何在不重复的情况下将列表中的项目与所有其他项目进行比较?

c# - 不要在 "using"语句后处理属性

c# - 如何清理 Entity Framework 对象上下文?

c# - 如何在 EF Core 表达式中使用继承的属性?