Suppress Entity Framework Core Queries logging for an specific query or context

Viewed 991

I have an ASP core MVC app and inside it a background task that run a query every 5 seconds, some things like this:

var dbContext = serviceProvider.GetRequiredService<ApplicationDbContext>();

var files = await dbContext.Set<File>().Where(...).ToListAsync();

The problem is it fills my output with this log:

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) ...

I know it's possible to change the global DB Context settings or log level to make ef log completely silent but I'm searching for a way to suppress log query of only this command or db context.

1 Answers

Well, I'm not sure if this is a good idea, but you can disable logging for a specific DbContext with something like that

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    base.OnConfiguring(optionsBuilder);

    optionsBuilder.UseLoggerFactory(LoggerFactory.Create(builder =>
    {
        builder.AddFilter(_ => false);
    }));
}
Related