How to view the SQL generated by the Entity Framework Core? Enable Sensitive Data Logging

  1. Install  Microsoft.Extensions.Logging.Debug  package
  2. Add for DbContextOptions:  .UseLoggerFactory(LoggerFactory.Create(builder => builder.AddDebug())) 
  3. Turn on Sensitive data for DbContextOptions:  .EnableSensitiveDataLogging() 

For those using Entity Framework Core, if you want to view the output SQL with sensitive data(parameter values) in the Visual Studio output window you may need to use Microsoft.Extensions.Logging.Debug package. This package allows you to turn on debug logging. You just need to add ‘.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddDebug()))’ in place where DbContextOptions is defined. And using the ‘EnableSensitiveDataLogging’ you will turn on sensitive output.

Add UseLoggerFactory and EnableSensistiveDataLogging()

Here is a code snippet with DbContextOptions configuration.  Pay attention to the lines 9 and 12.  DbContextOptions - enable Sensitive Data Logging

Window Output Example

Entity Framework - generated SQL in the Window Output

Watch my Video for more details

Used packages and Artifacts

  • Microsoft.Entityframeworkcore 6.0.8
  • Microsoft.Extensions.Logging.Debug 6.0.0
  • Visual Studio 2022

Read Also

Leave a Comment