日志记录Logging

提示

可在 GitHub 上查看此文章的示例You can view this article's sample on GitHub.

ASP.NET Core 应用程序ASP.NET Core applications

只要使用 AddDbContextAddDbContextPool,EF Core 就会自动与 ASP.NET Core 的日志记录机制集成。EF Core integrates automatically with the logging mechanisms of ASP.NET Core whenever AddDbContext or AddDbContextPool is used. 因此,在使用 ASP.NET Core 时,应按 ASP.NET Core 文档中的说明配置日志记录。Therefore, when using ASP.NET Core, logging should be configured as described in the ASP.NET Core documentation.

其他应用程序Other applications

EF Core 日志记录当前需要一个本身配置有一个或多个 ILoggerProvider 的 ILoggerFactory。EF Core logging currently requires an ILoggerFactory which is itself configured with one or more ILoggerProvider. 以下包中随附有常见提供程序:Common providers are shipped in the following packages:

备注

下面的代码示例使用ConsoleLoggerProvider已过时版本 2.2 中的构造函数。The following code sample uses a ConsoleLoggerProvider constructor that has been obsoleted in version 2.2. 适当替换已过时的日志记录 Api 将在版本 3.0 中提供。Proper replacements for obsolete logging APIs will be available in version 3.0. 在此期间,则可以安全地忽略,并禁止显示警告。In the meantime, it is safe to ignore and suppress the warnings.

安装相应的程序包之后, 该应用程序应创建 LoggerFactory 的单一实例/全局实例。After installing the appropriate package(s), the application should create a singleton/global instance of a LoggerFactory. 例如,使用控制台记录器:For example, using the console logger:

public static readonly LoggerFactory MyLoggerFactory
    = new LoggerFactory(new[] {new ConsoleLoggerProvider((_, __) => true, true)});

然后,应该在 DbContextOptionsBuilder 上向 EF Core 注册此单一实例/全局实例。This singleton/global instance should then be registered with EF Core on the DbContextOptionsBuilder. 例如:For example:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .UseLoggerFactory(MyLoggerFactory) // Warning: Do not create a new ILoggerFactory instance each time
        .UseSqlServer(
            @"Server=(localdb)\mssqllocaldb;Database=EFLogging;Trusted_Connection=True;ConnectRetryCount=0");

警告

应用程序不应为每个上下文实例创建新的 ILoggerFactory 实例,这一点非常重要。It is very important that applications do not create a new ILoggerFactory instance for each context instance. 这样做会导致内存泄漏和性能下降。Doing so will result in a memory leak and poor performance.

筛选记录的内容Filtering what is logged

备注

下面的代码示例使用ConsoleLoggerProvider已过时版本 2.2 中的构造函数。The following code sample uses a ConsoleLoggerProvider constructor that has been obsoleted in version 2.2. 适当替换已过时的日志记录 Api 将在版本 3.0 中提供。Proper replacements for obsolete logging APIs will be available in version 3.0. 在此期间,则可以安全地忽略,并禁止显示警告。In the meantime, it is safe to ignore and suppress the warnings.

筛选所记录内容的最简单方法是在注册 ILoggerProvider 时对其进行配置。The easiest way to filter what is logged is to configure it when registering the ILoggerProvider. 例如:For example:

public static readonly LoggerFactory MyLoggerFactory
    = new LoggerFactory(new[]
    {
        new ConsoleLoggerProvider((category, level)
            => category == DbLoggerCategory.Database.Command.Name
               && level == LogLevel.Information, true)
    });

在此示例中,筛选日志以仅返回以下位置的消息:In this example, the log is filtered to return only messages:

  • 在 Microsoft.EntityFrameworkCore.Database.Command 类别中in the 'Microsoft.EntityFrameworkCore.Database.Command' category
  • 在信息级别at the 'Information' level

对于 EF Core 记录器类别在中定义DbLoggerCategory类,以便可以方便地查找类别,但这些解析为简单的字符串。For EF Core, logger categories are defined in the DbLoggerCategory class to make it easy to find categories, but these resolve to simple strings.

有关基础日志记录基础结构的更多详细信息,请参阅 ASP.NET Core 日志记录文档More details on the underlying logging infrastructure can be found in the ASP.NET Core logging documentation.