我需要在EF Core中记录sql命令(选择,更新,删除,插入)。我有下一个认识:
public class CustomLoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName)
{
if (string.Equals(categoryName, DbLoggerCategory.Database.Command.Name))
{
return new CustomLogger();
}
return NullLogger.Instance;
}
public void Dispose() { }
private class CustomLogger : ILogger
{
public IDisposable BeginScope<TState>(TState state) => null;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var log = formatter(state, exception);
AppLogger.Log(log);
}
}
}
...
dbcontext.GetService<ILoggerFactory>().AddProvider(new CustomLoggerProvider());
在使用INSERT命令的情况下,我会得到一组日志。其中一个是下一个:
"Executing DbCommand [Parameters=[@p0='?' (Size = 128), @p1='?' (DbType = DateTimeOffset), @p2='?' (Size = 128), @p3='?' (Size = 128), @p4='?' (Size = 128) (DbType = Binary), @p5='?' (DbType = DateTimeOffset), @p6='?' (DbType = DateTimeOffset)], CommandType='Text', CommandTimeout='30']\r\nSET NOCOUNT ON;\r\nINSERT INTO [someSchema].[SomeTable] ([ID], [Column1], [Column2], [Column3], [Column4], [Column5], [Column6])\r\nVALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6);"
您可以看到@ p0 ='?'或@ p0而不是参数值。有办法解决吗?
答案 0 :(得分:0)
您需要启用Sensitive Logging
services.AddDbContextPool<MyDbContext>(builder =>
{
....
builder.EnableSensitiveDataLogging(true);
});
或
public class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.EnableSensitiveDataLogging();
base.OnConfiguring(optionsBuilder);
}
}