CosmosDb EF Core logging provider

I am trying to configure logging for the Cosmos DB Entity Framework Core provider to measure Request Units (RU) for performance. I am using .NET Core 3.1.

To enable logging for the provider, I need to use either the host logging configuration or configure it in Startup.cs or appsettings.{env}.json.

Host logging configuration

To use the host logging configuration, I need to add the following code to CreateHostBuilder() in Program.cs:

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

Configure in Startup.cs

Alternatively, I can configure logging in Startup.cs in ConfigureServices():

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>();
}

Configure in appsettings.{env}.json

Lastly, I can configure logging in appsettings.{env}.json:

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

I need to configure logging for the Cosmos DB Entity Framework Core provider to measure Request Units (RU) for performance. I am using .NET Core 3.1.

To enable logging for the provider, I can use either the host logging configuration, configure it in Startup.cs, or configure it in appsettings.{env}.json.

Host logging configuration

To use the host logging configuration, I need to add the following code to CreateHostBuilder() in Program.cs:

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

Configure in Startup.cs

Alternatively, I can configure logging in Startup.cs in ConfigureServices():

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>();
}

Configure in appsettings.{env}.json

Lastly, I can configure logging in appsettings.{env}.json:

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

To configure logging for the Cosmos DB Entity Framework Core provider, I can use either the host logging configuration, configure it in Startup.cs, or configure it in appsettings.{env}.json.

To measure Request Units (RU) for performance in the Cosmos DB Entity Framework Core provider using .NET Core 3.1, you can configure logging by using either the host logging configuration, configuring it in Startup.cs, or configuring it in appsettings.{env}.json.

For the host logging configuration, add the following code to CreateHostBuilder() in Program.cs:

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

Alternatively, you can configure logging in Startup.cs in ConfigureServices():

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>();
}

Lastly, you can configure logging in appsettings.{env}.json:

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