【问题标题】:How to configure .NET Core logging json file to ignore certain events?如何配置 .NET Core 日志记录 json 文件以忽略某些事件?
【发布时间】:2021-12-17 01:54:37
【问题描述】:

我想忽略 prod 系统中的某些警告。我得到的等效代码是这样的:

optionsBuilder
    .ConfigureWarnings(x => x.Ignore(RelationalEventId.MultipleCollectionIncludeWarning));

是否可以改为在appSettings.Production.json 中设置忽略?

目前是:

  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      "Microsoft.EntityFrameworkCore": "Warning",
      "IdentityServer4": "Information"
    }
  },

【问题讨论】:

    标签: asp.net-core logging .net-core entity-framework-core


    【解决方案1】:

    我不是 EF Core 专家,但很明显这是两种不同类型的配置;构建器调用告诉 EF Core 记录或不记录哪些事件,而 appsettings 只是指示日志记录框架在哪些级别监听哪些提供程序。您也许可以在给定的日志级别将整个提供程序静音,但这可能不够精细,无法仅过滤该提供程序中的某一类日志事件。

    如果 EF Core 没有用于从 Configuration 对象读取 Options 类的本机机制,并且您想要管理的开关集有限(即只有一组可以打开或一起关闭),然后您可以编写自己的来帮助管理它。

    配置类:

    public class CustomEfCoreLoggingOptions 
    {
        public const string Section = "CustomEfCoreLogging";
        public bool IgnoreMultipleCollectionIncludeWarning { get; set; }
    }
    

    应用设置:

    "CustomEfCoreLogging" : {
      "IgnoreMultipleCollectionIncludeWarning" : true
    }
    

    在您的创业公司中:

    var efLogging = Configuration.GetSection(CustomEfCoreLoggingOptions.Section).Get<CustomEfCoreLoggingOptions>();
    var optionsBuilder = new DbContextOptionsBuilder();
    
    if (efLogging.IgnoreMultipleCollectionIncludeWarning) optionsBuilder.ConfigureWarnings(x => x.Ignore(RelationalEventId.MultipleCollectionIncludeWarning));
    
    //...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 2010-09-13
      • 2012-03-19
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      相关资源
      最近更新 更多