Quartz.Net减少日志记录/删除特定的消息Serilog

时间:2020-06-23 13:56:43

标签: c# .net-core quartz.net serilog

我的Quartz.Net的.NET Core 3实现正在每分钟大约记录两次以下消息,我希望将其删除而不影响我的应用程序其他日志:

“批量获取0个触发器”

2020-06-22 17:42:24.745 +01:00 - [MyApplication] - [Debug] - [Quartz.Core.QuartzSchedulerThread] Batch acquisition of 0 triggers
2020-06-22 17:42:53.689 +01:00 - [MyApplication] - [Debug] - [Quartz.Core.QuartzSchedulerThread] Batch acquisition of 0 triggers

其中[MyApplication]Source属性填充,而[Quartz.Core.QuartzSchedulerThread]来自SourceContext

Quartz.net会自动记录此日志,我似乎无法决定是否记录它。我的日志填充得太快了。

我的 Appsettings.json 文件如下:

"Serilog": {
"IncludeScopes": false,
"MinimumLevel": {
  "Default": "Debug",
  "Override": {
    "Microsoft": "Warning",
    "System": "Warning"
  }
},
"WriteTo": [
  {
    "Name": "File",
    "Args": {
      "path": "C:/Logs/my-app.log",
      "buffered": "true",
      "flushToDiskInterval": "00:00:10",
      "rollingInterval": "Infinite",
      "rollOnFileSizeLimit": "true",
      "fileSizeLimitBytes": 10485760,
      "retainedFileCountLimit": 90,
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} - {Source} - [{Level}] - [{SourceContext}] {Message}{NewLine}{Exception}"
    }
  },
  {
    "Name": "Async",
    "Args": {
      "restrictedToMinimumLevel": "Information",
      "configure": [
        {
          "Name": "Console",
          "Args": {
            "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} - {Source} - [{Level}] - {Message}{NewLine}{Exception}"
          }
        }
      ]
    }
  }
]

}

我的 Program.cs main最初以如下方式配置记录器:

Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
            .Enrich.FromLogContext()
            // set Source property, which we use in output formatting, to the name of the application
            .Enrich.WithProperty("Source", value: "MyApplication")
            .CreateLogger();

然后在我的 Startup.cs 中,在注册了我的服务和设置后,Serilog的配置如下:

 public void Configure(IApplicationLifetime applicationLifetime)
    {
        SerilogLogger.Reconfigure(
            applicationName: "MyApplication",
            toFixedFile: !string.IsNullOrEmpty(_loggerSettings.LogFilePathFixed),
            fileName: _loggerSettings.LogFilePathFixed,
            fileSizeBytes: configuredLogFileSize * 1024 * 1024,
            minimalLevel: "Debug",
            outputTemplate: _loggerSettings.LogFormat);
    }

Reconfigure扩展方法来自另一个开发部门中内置的第三方dll。

在调用Reconfigure之后,我尝试添加以下内容:

SerilogLogger.RefineConfiguration(x => x.MinimumLevel.Override("Quartz", LogEventLevel.Warning));

但是它不起作用,因为实际的来源是MyApplication,如果我使用“ MyApplication”而不是“ Quartz”,则将取消整个应用程序的所有调试和信息日志。

有什么想法吗?请提供代码示例。

1 个答案:

答案 0 :(得分:0)

我已经针对此问题实施了解决方案,该解决方案包括两部分:

1- As pointed out to me in the Quartz google groups,我从appsettings.json中缺少以下Serilog配置来为Quartz定义正确的日志记录级别:

"MinimumLevel": {
  "Default": "Debug",
  "Override": {
    "Microsoft": "Warning",
    "System": "Warning",
    "Quartz": "Warning"
  }
},

"Quartz": "Warning"的配置禁止记录Debug“批量获取”消息。

2-完全不需要同时使用SerilogLogger.ReconfigureSerilogLogger.RefineConfiguration方法。我删除了这个。它覆盖了上面第1)点的配置。所有Seri​​log配置都应在我的应用程序设置中进行。