entity-framework/core/logging-events-diagnostics/extensions-logging.md
Microsoft.Extensions.Logging is an extensible logging mechanism with plug-in providers for many common logging systems. Both Microsoft-supplied plug-ins (e.g Microsoft.Extensions.Logging.Console) and third-party plug-ins (e.g. Serilog.Extensions.Logging) are available as NuGet packages.
Entity Framework Core (EF Core) fully integrates with Microsoft.Extensions.Logging. However, consider using simple logging for a simpler way to log, especially for applications that don't use dependency injection.
Microsoft.Extensions.Logging is used by default in ASP.NET Core applications. Calling xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext* or xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContextPool* makes EF Core automatically use the logging setup configured via the regular ASP.NET mechanism.
Other application types can use the GenericHost to get the same dependency injection patterns as are used in ASP.NET Core. xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext* or xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContextPool* can then be used just like in ASP.NET Core applications.
Microsoft.Extensions.Logging can also be used for applications that don't use dependency injection, although simple logging can be easier to set up.
Microsoft.Extensions.Logging requires creation of a xref:Microsoft.Extensions.Logging.LoggerFactory. This factory should be stored as a static/global instance somewhere and used each time a DbContext is created. For example, it is common to store the logger factory as a static property on the DbContext.
[!code-csharpMain]
This singleton/global instance should then be registered with EF Core on the xref:Microsoft.EntityFrameworkCore.DbContextOptionsBuilder. For example:
<!-- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder .UseLoggerFactory(MyLoggerFactory) .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFLogging;ConnectRetryCount=0"); -->[!code-csharpMain]
[!TIP] OnConfiguring is still called when AddDbContext is used or a DbContextOptions instance is passed to the DbContext constructor. This makes it the ideal place to apply context configuration regardless of how the DbContext is constructed.
By default, EF Core will not include the values of any data in exception messages. This is because such data may be confidential, and could be revealed in production use if an exception is not handled.
However, knowing data values, especially for keys, can be very helpful when debugging. This can be enabled in EF Core by calling xref:Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.EnableSensitiveDataLogging. For example:
<!-- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.EnableSensitiveDataLogging(); -->[!code-csharpEnableSensitiveDataLogging]
For performance reasons, EF Core does not wrap each call to read a value from the database provider in a try-catch block. However, this sometimes results in exceptions that are hard to diagnose, especially when the database returns a NULL when not allowed by the model.
Turning on xref:Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.EnableDetailedErrors* will cause EF to introduce these try-catch blocks and thereby provide more detailed errors. For example:
<!-- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.EnableDetailedErrors(); -->[!code-csharpEnableDetailedErrors]
The EF Core xref:Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.ConfigureWarnings* API allows applications to change what happens when a specific event is encountered. This can be used to:
Sometimes it can be useful to change the pre-defined log level for an event. For example, this can be used to promote two additional events from LogLevel.Debug to LogLevel.Information:
[!code-csharpChangeLogLevel]
In a similar way, an individual event can be suppressed from logging. This is particularly useful for ignoring a warning that has been reviewed and understood. For example:
<!-- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder .ConfigureWarnings(b => b.Ignore(CoreEventId.DetachedLazyLoadingWarning)); -->[!code-csharpSuppressMessage]
Finally, EF Core can be configured to throw for a given event. This is particularly useful for changing a warning into an error. (Indeed, this was the original purpose of ConfigureWarnings method, hence the name.) For example:
[!code-csharpThrowForEvent]
See Logging in .NET for guidance on log filtering and other configuration.
EF Core logging events are defined in one of:
These definitions contain the event IDs, log level, and category for each event, as used by Microsoft.Extensions.Logging.