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

2 thoughts on “How to view the SQL generated by the Entity Framework Core? Enable Sensitive Data Logging”

  1. Hello,
    what is difference between

    .UseLoggerFactory(LoggerFactory.Create(builder => builder.AddDebug()))
    options.LogTo(x => Debug.WriteLine(x), LogLevel.Information);

    it seems to do the same, output SQL queries to the output window in IDE.

    Thank you

    • Both methods serve the purpose of logging SQL queries executed by Entity Framework in C#. However, there are differences in how they achieve this logging functionality.

      .UseLoggerFactory(LoggerFactory.Create(builder => builder.AddDebug())):
      This method sets up a logger factory for Entity Framework and adds a debug logger to it. The LoggerFactory.Create method creates an instance of a logger factory, and the builder.AddDebug() method adds a debug logger to that factory. This logger will log the SQL queries to the debug output window.

      options.LogTo(x => Debug.WriteLine(x), LogLevel.Information):
      This method configures logging options directly on the options object of the DbContext or DbContextOptionsBuilder. The LogTo method specifies a delegate that will be invoked for each log message. In this case, the delegate is x => Debug.WriteLine(x), which means that the log messages will be output using the Debug.WriteLine method. The LogLevel.Information parameter specifies that only log messages with an information level or higher will be logged.

      Both methods essentially accomplish the same result of logging SQL queries to the output window in the IDE. However, the first method uses a separate logger factory and adds a debug logger to it, while the second method directly configures the logging options on the DbContext or DbContextOptionsBuilder. The choice between the two methods depends on personal preference and the specific requirements of your application.


Leave a Comment