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.
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 .
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.
Nested optional dependents sharing a table and with no required properties are disallowed.
EF Core 6.0 itself is 31% faster executing queries. Heap allocations have been reduced by 43%.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With