Logging Mechanisms Quick Reference

MechanismAsyncScopeRegisteredIntended Use
Simple loggingNoPer contextContext configurationDevelopment-time logging
Microsoft.Extensions.LoggingNoPer contextDI or context configurationProduction logging
EventsNoPer contextAny timeReaching to EF events
InterceptorsYesPer contextContext configurationManipulating EF operations
Diagnostics listenersNoProcessGloballyApplication diagnostics

Simple logging

Use LogTo when configuring a DbContext instance:

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

Microsoft.Extensions.Logging

Use like any ASP.NET Core application.

Events

Act as callbacks when an event occurs in EF Core. These are easier to use than interceptors, but they are synchronous only and cannot perform non-blocking async I/O.

Events are registered per DbContext instance. Use a diagnostic listener to get the same information but for all DbContext instances in the process.

Interceptors

Enable interception, modification, and/or suppression of EF Core operations, including low-level operations like executing a SQL command. If the only requirement is logging, interceptors are not the best choice.

Diagnostic listeners

Allow listening for EF Core events that occur in the current .NET process.