Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core EnableSensitiveDataLogging does not work as expected

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.

like image 577
pantonis Avatar asked Apr 04 '19 10:04

pantonis


People also ask

How do I enable EnableSensitiveDataLogging?

RE: Be sure to disable sensitive data logging when deploying to production. You can enable it conditionally, like: if (env. IsDevelopment()) { options. EnableSensitiveDataLogging(); } for Web (ASP.NET) applications, then env is IWebHostEnvironment .

When should you not use Efcore?

One of the biggest reasons not to use Entity Framework Core is that your application needs the fastest possible data access. Some applications do a lot of heavy data operations with very high-performance demands, but usually business applications don't have that high of a performance demand.

Is an optional dependent using table sharing without any required?

Nested optional dependents sharing a table and with no required properties are disallowed.

Is EF core faster than EF6?

EF Core 6.0 itself is 31% faster executing queries. Heap allocations have been reduced by 43%.


1 Answers

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.

like image 97
JRS Avatar answered Sep 18 '22 19:09

JRS