24

Entity Framework core by default logs all executed SQL queries to the ASP.NET Core logger (Microsoft.Extensions.Logging). The default log level is Informational, but it seems a bit chatty to me for informational logging. I would prefer it at Debug or even Trace level.

Is there any way to configure EFCore to log these SQL queries at Debug (or Trace) level, instead of Informational level?

3

2 Answers 2

26

Since Entity Framework Core 3.0 it's possible to change to logging level of SQL queries. During to 3.0 previews all query execution logging was changed to Debug by default. Later this change got reverted and now it's configurable.

To do this override OnConfiguring in your DbContext and run the following snippet:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder.ConfigureWarnings(c => c.Log((RelationalEventId.CommandExecuting, LogLevel.Debug)));

See: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#query-execution-is-logged-at-debug-level-reverted

Sign up to request clarification or add additional context in comments.

Comments

1

You can set log level of Microsoft or System messages individually.

For your EntityFramework chatty logging you can set this in your startup class.

services.AddLogging(builder =>
{
    builder.AddFilter("Microsoft", LogLevel.Warning);
    builder.AddFilter("System", LogLevel.Error);
});

1 Comment

Sorry, but that is not the question. You're setting a filter. I don't want to filter the messages, I want to change the log level of a message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.