首页 > 解决方案 > 如何在 EF Core 3 中启用日志记录?

问题描述

我正在使用 Entity Framework Core 3 Preview 5 和 ASP.NET Core 3 Preview 5。在 Visual Studio 2019 的调试输出窗口中,我没有收到来自 EF Core 的日志。我阅读了文档,但之后我更加困惑:

  1. 根据https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder.useloggerfactory?view=efcore-2.1日志记录应该自动设置:

使用“AddDbContext”方法之一时无需调用此方法。'AddDbContext' 将确保 EF 使用的 ILoggerFactory 是从应用程序服务提供者处获得的。

不过,这不是我的经验。

  1. 我尝试通过注入 to 来启用日志记录ILoggerFactoryConfigureServices我打算然后将其传递给DbContextOptionsBuilder.UseLoggerFactory,但这已经不可能了,请参阅https://github.com/aspnet/Announcements/issues/353

那么,如何将日志记录设置到 EF Core 3.0 中的调试输出窗口?谢谢!

标签: c#asp.net-coreentity-framework-core

解决方案


3.0 RTM 及更高版本的更新:日志级别恢复为信息。检查过滤文档中记录的内容以获取更多详细信息


票数接近可能是因为问题中没有可以重现问题的代码。

在任何情况下,EF Core 都会在调试级别记录。通用主机构建器或 Web 主机构建器使用的默认级别是Information. 日志级别必须更改为TraceDebug

默认情况下,此代码不会记录任何 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 事件的日志记录级别更改为TraceDebug通过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.

推荐阅读