【问题标题】:CosmosDb Entity Framework Core provider loggingCosmosDb Entity Framework Core 提供程序日志记录
【发布时间】:2021-01-16 00:18:54
【问题描述】:

我正在尝试获取 CosmosDb Entity Framework Core 提供程序的日志记录,以查看正在执行的查询。出于性能原因,我还想测量本地模拟器的请求单位。不使用 EF Core 有来自 CosmosDbClient 的响应,但在这种特定情况下,我希望它使用 EF Core CosmosDb 提供程序。

查看https://dotnetfalcon.com/entity-framework-core-cosmos-db-provider/ 似乎是可能的,但这种方式的日志记录已被弃用,甚至无法在 .NET Core 3.1 上编译

同样看源码,实现了DbLoggerCategory.Database.Command的IDiagnisticsLogger https://github.com/aspnet/EntityFrameworkCore/blob/9abd61d3fb4cdd1041f25fe2e10c70ee3dc4055e/src/EFCore.Cosmos/Diagnostics/Internal/CosmosLoggerExtensions.cs

我在 Startup.cs 中尝试了以下操作,但遗憾的是控制台中没有打印任何查询。

public void ConfigureServices(IServiceCollection services)
{
    _applicationOptions = GetApplicationOptions(services);

    services.AddDbContext<MyDbContext>(options =>
    {
        options.UseCosmos(_applicationOptions.AccountEndpoint, _applicationOptions.AccountKey, "MySecretDb");
        options.UseLoggerFactory(GetLoggerFactory());
    });

    ...
}

private ILoggerFactory GetLoggerFactory()
{
    IServiceCollection serviceCollection = new ServiceCollection();
    if (Environment.IsDevelopment())
    {
        serviceCollection.AddLogging(builder =>
            builder.AddConsole()
                .AddFilter(DbLoggerCategory.Database.Command.Name, Microsoft.Extensions.Logging.LogLevel.Debug));
    }
    return serviceCollection.BuildServiceProvider()
        .GetService<ILoggerFactory>();
}

代码配置有效

使用没有上述代码的主机日志配置,但我宁愿在 Startup.cs 或 appsettings.{env}.json 中配置它

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureLogging(logging =>
            {
                logging.AddFilter(DbLoggerCategory.Database.Command.Name, LogLevel.Debug);
                logging.AddConsole();
            })

配置中的日志记录部分不起作用

我已经在我的开发 json 中尝试过这个,它应该可以工作,但不知何故不起作用

{
  "Logging": {
    "Console": {
      "IncludedScopes": true,
      "LogLevel": {
        "Microsoft.EntityFrameworkCore.Database.Command": "Debug"
      },
      "Default": "Information"
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },

如何使用采用 appsettings.{env}.json 的 CreateDefaultBinder 使其工作?

【问题讨论】:

    标签: entity-framework-core entity-framework-core-cosmosdb


    【解决方案1】:

    您应该在配置的 LogLevel 部分中指定"Microsoft.EntityFrameworkCore": "Debug"。 但是您使用的更具体的过滤器应该可以工作。这是我尝试过的:

      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Information",
          "Microsoft.Hosting.Lifetime": "Information",
          "Microsoft.EntityFrameworkCore.Database.Command": "Debug"
        }
      }
    

    有一点很重要:我没有指定自定义记录器工厂,默认的是原子注入的。在手动创建记录器工厂的情况下,要从配置系统(包括 json 文件)加载配置,您需要具备以下条件:

    loggingBuilder.AddConfiguration(configuration.GetSection("Logging"));
    

    这正是CreateDefaultBuilder 为默认日志工厂所做的。

    【讨论】:

    • 你回答太晚了一年;)我不知道你的答案是否正确,我自己早就回答了这个问题。还是谢谢
    • 对不起,也许它对其他人有用:)
    猜你喜欢
    • 1970-01-01
    • 2017-01-10
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    相关资源
    最近更新 更多