Simple Logging

Improve EF Core performance with EF Extensions

Simple Logging

Simple Logging is the equivalent of Database.Log in EF6. It provides a simple way to get logs from EF Core without the need to configure any kind of external logging framework.

EF Core replaces Database.Log with a LogTo method called on DbContextOptionsBuilder in either AddDbContext or OnConfiguring.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder.LogTo(Console.WriteLine);

There are multiple overloads available which can be used in different use cases.

  • Set the minimum log level

    • Example: .LogTo(Console.WriteLine, LogLevel.Information)

  • Filter for only specific events:

    • Example: .LogTo(Console.WriteLine, new[] {CoreEventId.ContextInitialized, RelationalEventId.CommandExecuted})

  • Filter for all events in specific categories:

    • Example: .LogTo(Console.WriteLine, new[] {DbLoggerCategory.Database.Name}, LogLevel.Information)

  • Use a custom filter over event and level:

    • Example: .LogTo(Console.WriteLine, (id, level) => id == RelationalEventId.CommandExecuting)

Output format can be minimally configured (API is in flux) but the default output looks something like:

Last updated