entity-framework – 抑制在Entity Framework核心中登录的SQL查询

前端之家收集整理的这篇文章主要介绍了entity-framework – 抑制在Entity Framework核心中登录的SQL查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用实体框架核心的控制台.net核心应用程序.
该应用程序使用日志框架写入文件和控制台:
  1. serviceProvider = new ServiceCollection()
  2. .AddLogging()
  3. .AddDbContext<DataStoreContext>(options =>
  4. options.UsesqlServer(Configuration.GetConnectionString("DefaultConnection")))
  5. .BuildServiceProvider();
  6.  
  7. //configure console logging
  8. serviceProvider.GetService<ILoggerFactory>()
  9. .AddConsole(LogLevel.Debug)
  10. .AddSerilog();
  11.  
  12. Log.Logger = new LoggerConfiguration()
  13. .MinimumLevel.Information()
  14. .WriteTo.RollingFile(Path.Combine(Directory.GetCurrentDirectory(),"logs/vcibot-{Date}.txt"))
  15. .WriteTo.RollingFile(Path.Combine(Directory.GetCurrentDirectory(),"logs/vcibot-errors-{Date}.txt"),LogEventLevel.Error)
  16. .CreateLogger();
  17.  
  18. logger = serviceProvider.GetService<ILoggerFactory>()
  19. .CreateLogger<Program>();

文件输出的最低级别设置为“信息”.但是这个设置输出也包含SQL查询,这里是一个例子:

2017-02-06 10:31:38.282 -08:00 [Information] Executed DbCommand (0ms)
[Parameters=[],CommandType=’Text’,CommandTimeout=’30’] SELECT
[f].[BuildIdentifier],[f].[Branch],[f].[BuildDate],
[f].[StaticAssetSizeInKb] FROM [FileSizesHistoryEntries] AS [f]

有没有办法禁用SQL查询日志记录(仅在调试日志级别记录它们)

解决方法

如果您使用的是内置记录器,则可以在Program.cs中为ILoggingBuilder添加过滤器.

所以,它看起来像:

  1. WebHost.CreateDefaultBuilder(args)
  2. // ...
  3. .ConfigureLogging((context,logging) => {
  4. var env = context.HostingEnvironment;
  5. var config = context.Configuration.GetSection("Logging");
  6. // ...
  7. logging.AddConfiguration(config);
  8. logging.AddConsole();
  9. // ...
  10. logging.AddFilter("Microsoft.EntityFrameworkCore.Database.Command",LogLevel.Warning);
  11. })
  12. // ...
  13. .UseStartup<Startup>()
  14. .Build();

猜你在找的MsSQL相关文章