【发布时间】: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