如何在EF Core 3中启用日志记录? [英] How to enable logging in EF Core 3?

查看:231
本文介绍了如何在EF Core 3中启用日志记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Entity Framework Core 3 Preview 5 and ASP.NET Core 3 Preview 5. In my Debug Output Window of Visual Studio 2019 I get no logs from EF Core. I read the documentation, but after that I am even more confused:

  1. 根据 https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder.useloggerfactory?view=efcore-2.1 日志记录应自动设置:
  1. According to https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder.useloggerfactory?view=efcore-2.1 logging should be setup automatically:

使用"AddDbContext"方法之一时,无需调用此方法. "AddDbContext"将确保从应用程序服务提供商处获得EF使用的ILoggerFactory.

There is no need to call this method when using one of the 'AddDbContext' methods. 'AddDbContext' will ensure that the ILoggerFactory used by EF is obtained from the application service provider.

那不是我的经验.

  1. 我试图通过将ILoggerFactory注入到ConfigureServices来启用日志记录(我打算将其传递给DbContextOptionsBuilder.UseLoggerFactory,但是现在已经不可能了,请参见
  1. I tried to enable logging by injecting the ILoggerFactory to ConfigureServices (I intended to then pass it on to DbContextOptionsBuilder.UseLoggerFactory, but that's not possible anymore, see https://github.com/aspnet/Announcements/issues/353

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

So, how do I setup logging to the debug output windows in EF Core 3.0? Thanks!

推荐答案

3.0 RTM和更高版本的更新:日志级别恢复为信息".检查过滤什么是在文档中登录以获取更多详细信息

Update for 3.0 RTM and later: The log level reverted to Information. Check filtering what is logged in the docs for more details

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

The close votes are probably because there's no code in the question that can reproduce the problem.

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

In any case, EF Core logs at the Debug level. The default level used by generic host builder or the Web host builder is Information. The logging level will have to be changed to Trace or Debug.

默认情况下,此代码不会记录任何EF事件:

By default, this code won't log any EF events :

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事件,我们需要通过appsettings.json或代码将EF Core事件的记录级别更改为TraceDebug.例如,将其包括在appsettings.json中:

To log EF events we need to change the logging level for EF Core events to Trace or Debug through appsettings.json or code. For example, including this in 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.

这篇关于如何在EF Core 3中启用日志记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆