【问题标题】:Serilog Filtering not working in appsettings.jsonSerilog 过滤在 appsettings.json 中不起作用
【发布时间】:2022-07-07 10:30:15
【问题描述】:

我实现了 Serilogger 并将配置放在 Program.cs 中,效果很好。我正在尝试将配置移动到 appsettings.json 并将其写入日志,但我现在无法让过滤器工作。我知道它在读取配置,因为我可以更改最低日志级别并在日志中看到结果。

我一直在阅读和重读 Github 中的 Serilog 页面,尤其是 configuration page

这是在 Program.cs 中工作的代码

            try
            {
                Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));



                var levelSwitch = new LoggingLevelSwitch();
                levelSwitch.MinimumLevel = LogEventLevel.Warning;

                return new LoggerConfiguration()
                    .ReadFrom.Configuration(config, "Serilog");

                    .MinimumLevel.ControlledBy(levelSwitch)
                    .Enrich.FromLogContext()
                    .WriteTo.Debug()
                    .WriteTo.Logger(l => l
                        .Filter.ByExcluding("source = 'application'")
                        .WriteTo.File("Logs\\log-.txt", rollingInterval: RollingInterval.Day))
                    .WriteTo.Logger(l => l
                        .Filter.ByIncludingOnly("source='customer'")
                        .WriteTo.MSSqlServer(
                            connectionString:
                            "Server=(localdb)\\Infinity;Database=Infinity;Trusted_Connection=True;MultipleActiveResultSets=True;",
                            sqlSinkOptions));
            }

这是新的 appsettings 文件

{
  "Serilog": {
    "Using": [ "Serilog.Enrichers.FromLogContext" ],
    "MinimumLevel": "Warning",
    "WriteTo": [
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "Server=(localdb)\\Infinity;Database=Infinity;Trusted_Connection=True;MultipleActiveResultSets=True;",
          "tableName": "Log",
          "Filter": [
            {
              "Name": "ByIncludingOnly",
              "Args": {
                "expression": "source = 'customer'"
              }
            }
          ]
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "Logs\\log-.txt",
          "rollingInterval": "Day",
          "retainedFileCountLimit": 7,
          "Filter": [
            {
              "Name": "ByExcluding",
              "Args": {
                "expression": "source = 'customer'"
              }
            }
          ]
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  },

更新:我发现我正在过滤的自定义属性不再插入到记录器对象中。我认为这与“丰富者”有关吗?但它不是过滤器问题的根源,因为如果属性不存在,“IncludeOnly”过滤器不应该写任何东西,对吗?

这是我使用 PushProperty 插入属性和值的方法中的一个 sn-p。由于我没有接触过这段代码,我认为问题仍然存在于应用设置中。


        public void LogError(LogDestination destination, LogLevel level, string msg)
        {
            if (destination == LogDestination.Customer)
            {                
                using (LogContext.PushProperty("source", "customer")) {
                    switch (level)
                    {
                        case LogLevel.Debug:
                            _logger.LogInformation(msg);
                            break;

我也尝试将 .Enrich 添加回 LoggerConfiguration 中,没有任何区别。

  return new LoggerConfiguration()
                    .ReadFrom.Configuration(config, "Serilog")
                    .Enrich.FromLogContext();

更新我在顶层添加了一个全局过滤器。我尝试了 Include Only 和 Exclude ,两者都按预期工作。我不知道为什么水槽过滤器不起作用。

{
  "Serilog": {
    "Using": [ "Serilog.Expressions", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer" ],
    "MinimumLevel": "Warning",
    "Filter": [
      {
        "Name": "ByExluding",
        "Args": {
          "expression": "source = 'customer'"
        }
      }
    ],

【问题讨论】:

    标签: c# serilog-filter


    【解决方案1】:

    我尝试在 Appsettings 文件中使用过滤器而不是过滤器,它对我有用。但是没有看到任何关于它的文档。

    {
      "Serilog": {
        "Using": [ "Serilog.Expressions", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer" ],
        "MinimumLevel": "Warning",
        "Filters": [
          {
            "Name": "ByExluding",
            "Args": {
              "expression": "source = 'customer'"
            }
          }
        ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      相关资源
      最近更新 更多