加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 06:30:27 所属栏目:MsSql教程 来源:网络整理
导读:我有一个使用实体框架核心的控制台.net核心应用程序. 该应用程序使用日志框架写入文件和控制台: serviceProvider = new ServiceCollection() .AddLogging() .AddDbContextDataStoreContext(options = options.UseSqlServer(Configuration.GetConnectionStrin
我有一个使用实体框架核心的控制台.net核心应用程序.
该应用程序使用日志框架写入文件和控制台:
serviceProvider = new ServiceCollection()
        .AddLogging()
        .AddDbContext<DataStoreContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")))
        .BuildServiceProvider();

    //configure console logging
    serviceProvider.GetService<ILoggerFactory>()
        .AddConsole(LogLevel.Debug)
        .AddSerilog();

    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Information()
        .WriteTo.RollingFile(Path.Combine(Directory.GetCurrentDirectory(),"logs/vcibot-{Date}.txt"))
        .WriteTo.RollingFile(Path.Combine(Directory.GetCurrentDirectory(),"logs/vcibot-errors-{Date}.txt"),LogEventLevel.Error)
        .CreateLogger();

    logger = serviceProvider.GetService<ILoggerFactory>()
        .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添加过滤器.

所以,它看起来像:

WebHost.CreateDefaultBuilder(args)
    // ...
    .ConfigureLogging((context,logging) => {
        var env = context.HostingEnvironment;
        var config = context.Configuration.GetSection("Logging");
        // ...
        logging.AddConfiguration(config);
        logging.AddConsole();
        // ...
        logging.AddFilter("Microsoft.EntityFrameworkCore.Database.Command",LogLevel.Warning);
    })
    // ...
    .UseStartup<Startup>()
    .Build();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读