我的 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
扩展方法来自另一个开发部门构建的第 3 方 dll。
我尝试在调用
Reconfigure
后添加以下内容:
SerilogLogger.RefineConfiguration(x => x.MinimumLevel.Override("Quartz", LogEventLevel.Warning));
但它不起作用,因为实际源是
MyApplication
,如果我使用“MyApplication”而不是“Quartz”,我会抑制整个应用程序的所有调试和信息日志。
有什么想法吗?请提供代码示例。
我已经针对这个问题实现了一个解决方案,该解决方案有两个部分:
1 -正如 Quartz google 群组中向我指出的那样,我缺少 appsettings.json 中的以下 Serilog 配置来定义 Quartz 的正确日志记录级别:
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning",
"Quartz": "Warning"
}
},
"Quartz": "Warning"
的配置抑制了Debug
“批量获取”消息的记录。
还有
2 - 完全没有必要调用
SerilogLogger.Reconfigure
和 SerilogLogger.RefineConfiguration
方法。我删除了这个。它覆盖了上面第 1) 点的配置。所有 Serilog 配置都应该在我的应用程序设置中进行。
如果 Serilog 配置是在 appsettings.json(例如 Program.cs)之外设置的,则对已接受答案的补充是:
Log.Logger = new LoggerConfiguration()
// [other configs omitted]
.MinimumLevel.Override("Quartz", LogEventLevel.Warning)
.CreateLogger();