EF Core EnableSensitiveDataLogging does not work as expected

.net .net-core asp.net-core c# entity-framework-core

Question

I am using EF Core 2.1.1 using MySql and I have the following code to enable logging of the ef core translated to SQL Queries

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
      optionsBuilder.EnableSensitiveDataLogging();
}

I don't see anything in the logs though and I don't know why.

1
1
4/4/2019 10:21:57 AM

Popular Answer

Using EF Core 2.2 with Visual Studio 2019 and SQL Server, I was able to get the EnableSensitiveDataLogging option to work using the following configuration.

In your Startup.cs class,

Add the logging service to your ConfigureServices method. I'm filtering the logging to database commands and information logging level,

services.AddLogging(loggingBuilder => {
    loggingBuilder.AddConsole()
        .AddFilter(DbLoggerCategory.Database.Command.Name, LogLevel.Information);
    loggingBuilder.AddDebug();
});

Now update your DBContext to enable the sensitive data logging,

services.AddDbContext<MyDbContext>(options => {
    options.UseSqlServer(_configuration.GetConnectionString("MyDbConnection"));
    options.EnableSensitiveDataLogging(true);
});

Once you've configured your startup, you will now see the SQL commands and their sensitive data appear in our Output window in Visual Studio (Debug...Windows...Output) as shown below,

Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (3ms) 
    [Parameters=[@p1='aaa' (Nullable = false) (Size = 450), 
                 @p2='bbb' (Size = 4000), 
                 @p0='New Column Value' (Size = 4000)], 
    CommandType='Text', CommandTimeout='30']

SET NOCOUNT ON;
UPDATE [MyTableName] SET [MyColumnName] = @p0
WHERE [Id] = @p1 AND [OtherColumn] = @p2;
SELECT @@ROWCOUNT;

It took me a while to figure this out as well. I could not get the sensitive data to appear in the log using the OnConfiguring in the DBContext class either.

Hope this helps even though it's using SQL Server and not MySQL.

NOTE! Be sure to disable sensitive data logging when deploying to production.

2
9/6/2019 5:34:05 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow