Suppress EF core 3.0.x initialized msg

2019-07-29 12:51发布

问题:

I have:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
{
  string connectionString = "mydb.db;";
  optionsBuilder
  .UseLoggerFactory(MainWorker.ConsoleLoggerFactory)
  .EnableSensitiveDataLogging(true)
  .UseSqlite(connectionString);
}

Whenever I access my DBContext the console shows

info: Microsoft.EntityFrameworkCore.Infrastructure[10403] Entity Framework Core 3.0.0-preview4.19176.6 initialized

Is there a way to filter out this particular message ? as I do a lot of queries it just clutters my console debug window ..

回答1:

EF Core specific log messages are configured via DbContextOptionsBuilder.ConfigureWarnings method (yeah, the name is a bit misleading).

The EventId of the message in question is CoreEventId.ContextInitialized. And you suppress it using Ignore:

optionsBuilder.ConfigureWarnings(warnings => warnings
    .Ignore(CoreEventId.ContextInitialized));

Of course it can be chained with the other optionsBuilder calls. Also you may want to suppress the second context lifetime related log message with CoreEventId.ContextDisposed.