-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Closed as not planned
Closed as not planned
Copy link
Labels
Description
Usually EF Core logs SQL queries with parameters, so I can enable or disable sensitive data logging to see or not see query parameters.
Here is my OnConfiguring implemetation with disabled sensitive data logging:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (DatabaseLoggingEnabled)
{
if (_loggerFactory == null)
{
_loggerFactory = CreateLoggerFactory(optionsBuilder, _logManager, _configuration.GetSection("Logging"));
}
optionsBuilder.UseLoggerFactory(_loggerFactory);
optionsBuilder.EnableSensitiveDataLogging(false);
}
}Here is an example log output for update single record (parameter values masked by '?'):
Executed DbCommand (5ms) [Parameters=[@p5='?' (DbType = Int32), @p0='?' (DbType = Int32), @p1='?' (DbType = DateTime2), @p2='?' (DbType = Boolean), @p3='?' (Size = 4000), @p6='?' (Size = 8) (DbType = Binary), @p4='?' (DbType = DateTime2)], CommandType='Text', CommandTimeout='30']
SET NOCOUNT ON;
UPDATE [Models] SET [Code] = @p0, [CreatedOn] = @p1, [IsActive] = @p2, [Name] = @p3, [UpdatedOn] = @p4
WHERE [Id] = @p5 AND [RowVersion] = @p6;
SELECT [RowVersion]
FROM [Models]
WHERE @@ROWCOUNT = 1 AND [Id] = @p5;
But when I use ExecuteUpdate extension, there are no sql parameters and I can't mask sensitive data.
Here is an ExecuteUpdate example:
var keys = Enumerable.Range(1, 10);
await _dbContext
.Models
.Where(e => keys.Contains(e.Id))
.ExecuteUpdateAsync(s => s
.SetProperty(e => e.Code, 123)
.SetProperty(e => e.IsActive, true)
.SetProperty(e => e.UpdatedOn, DateTime.UtcNow));Here is the log output:
Executed DbCommand (2,844ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
UPDATE [m]
SET [m].[UpdatedOn] = '2023-07-31T14:55:14.0287614Z',
[m].[IsActive] = CAST(1 AS bit),
[m].[Code] = 123
FROM [Models] AS [m]
WHERE [m].[Id] IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
You can see values are inlined in SQL.
Can ExecuteUpdate method gerenerate SQL via parameters but not inline values?
Reactions are currently unavailable