英文:
CosmosDb Entity Framework Core provider logging
问题
我正在尝试为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使其正常工作?
英文:
I'm trying to get logging for the CosmosDb Entity Framework Core provider to see the queries that are being executed. I also want to measure the Request Units for performance reasons to the local emulator. Not using EF Core there is a response from the CosmosDbClient, but in this specific case I want it using
the EF Core CosmosDb provider.
Looking at https://dotnetfalcon.com/entity-framework-core-cosmos-db-provider/ it seems possible, but logging that way is deprecated and doesn't even compile on .NET Core 3.1
Also looking at the source, the IDiagnisticsLogger for DbLoggerCategory.Database.Command is implemented
https://github.com/aspnet/EntityFrameworkCore/blob/9abd61d3fb4cdd1041f25fe2e10c70ee3dc4055e/src/EFCore.Cosmos/Diagnostics/Internal/CosmosLoggerExtensions.cs
I tried the following in Startup.cs, but sadly there is no query printed in the console.
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>();
}
Code configuration works
Use the host logging configuration it works without the code above, but I do rather configure it in Startup.cs or else in appsettings.{env}.json
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.AddFilter(DbLoggerCategory.Database.Command.Name, LogLevel.Debug);
logging.AddConsole();
})
Logging section in config doesn't work
I have tried this in my development json, which should work but somehow doesn't work
{
"Logging": {
"Console": {
"IncludedScopes": true,
"LogLevel": {
"Microsoft.EntityFrameworkCore.Database.Command": "Debug"
},
"Default": "Information"
},
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
How to get it working using CreateDefaultBinder which takes the appsettings.{env}.json ?
答案1
得分: 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
为默认的日志记录工厂所做的事情。
英文:
You should specify "Microsoft.EntityFrameworkCore": "Debug"
for in the LogLevel section of your configuration.
But even more specific filter you use should work. Here is what I tried:
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft.EntityFrameworkCore.Database.Command": "Debug"
}
}
One important point: I don't specify custom logger factory, the default one is injected atomatically. For the configuration to be loaded from the config system (incl. json file) in case of manual creation of logger factory you need to have this:
loggingBuilder.AddConfiguration(configuration.GetSection("Logging"));
This is exactly what CreateDefaultBuilder
does for the default logging factory.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论