dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.63k stars 3.15k forks source link

Empty formatted output string for connection debug log #29527

Open Tornhoof opened 1 year ago

Tornhoof commented 1 year ago

After upgrade to EF Core 7/.NET 7.0, running a simple db app with debug logging, I noticed the following that the disposed log message does not contain any values, just empty placeholders. The log entry just above still contains the values.

dbug: Microsoft.EntityFrameworkCore.Infrastructure[10407]
      'MyContext' disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20007]
      Disposing connection to database 'Repro' on server 'localhost'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20008]
      Disposed connection to database '' on server '' (1ms).

Repro:

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

const string connectionString = "Data Source=localhost;Initial Catalog=Repro;TrustServerCertificate=True;Integrated Security=True;";

var builder = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddLogging();
        services.AddDbContext<MyContext>(a => a.UseSqlServer(connectionString));
    })
    .ConfigureLogging(logging =>
    { 
        logging.ClearProviders();
        logging.AddConsole();
        logging.SetMinimumLevel(LogLevel.Debug);
    });
using var host = builder.Build();

await host.StartAsync();
await using var context = host.Services.GetRequiredService<MyContext>();
await context.Database.EnsureCreatedAsync().ConfigureAwait(false);

await context.DisposeAsync().ConfigureAwait(false);
await host.StopAsync();

Console.ReadLine();

public class MyContext : DbContext
{
    public MyContext(DbContextOptions options) : base(options)
    {

    }
}

Include provider and version information

EF Core version: 7.0.0 Database provider:: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 7 Operating system: Win11 IDE: VS 17.4

ajcvickers commented 1 year ago

Note for triage: we added these log messages in EF7 (#27920), but, at least on some providers, the database name and server are cleared before this message is logged. We could remove the parameters, or display a different message when they are not available, or record them earlier so they are always available. Or we could do nothing and leave the message as-is for providers where these things are no longer available.