Quartz.Net Reduce Logging / Remove Specific Message Serilog - c#

My .NET Core 3 implementation of Quartz.Net is logging the following message approximately twice per minute and I would like to remove it without affecting my applications other logs:
"Batch acquisition of 0 triggers"
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
Where [MyApplication] is populated by the Source property and [Quartz.Core.QuartzSchedulerThread] comes from the SourceContext.
Quartz.net logs this automatically and I seem to have no control over deciding to log it or not. My logs are filling up too quickly.
My Appsettings.json file is as follows:
"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}"
}
}
]
}
}
]
}
My Program.cs main initially configures the logger like this:
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();
And later in my Startup.cs, when my services & settings have been registered Serilog is reconfigured as follows:
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);
}
Where the Reconfigure extension method comes from a 3rd party dll built in another development department.
I have tried to add the following after the invocation of Reconfigure:
SerilogLogger.RefineConfiguration(x => x.MinimumLevel.Override("Quartz", LogEventLevel.Warning));
But it doesn't work because the actual source is MyApplication, and if I use "MyApplication" instead of "Quartz" I suppress all debug and information logs for the entire application.
Any ideas? With code examples please.

I have implemented a solution for this problem, there were two parts to the solution:
1 - As pointed out to me in the Quartz google groups, I was missing the following Serilog configuration from appsettings.json to define the correct logging level for Quartz:
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning",
"Quartz": "Warning"
}
},
The configuration of "Quartz": "Warning" suppressed the Debug "Batch acquisition" messages from being logged.
And
2 - The invocation of both the SerilogLogger.Reconfigure and SerilogLogger.RefineConfiguration methods was completely unnecessary. I removed this. It was overwriting the config from point 1) above. All Serilog configuration should take place in my appsettings.

Related

Serilog filename help asp.net core C#

Am new to serilog but I cannot see how to set the name of the log being generated by Serilog.
I am using the ASP.net core and have registered serilog and using the appsettings.json to log into my file which works as expected.
However the name of the file isn't adhering to the WriteTo configuration as defined:
"Serilog": {
"Using": [
"Serilog.Sinks.Console"
],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Debug",
"System": "Debug"
}
},
"Enrich": [
"FromLogContext",
"WithExceptionDetails",
"WithEnvironmentUserName",
"WithMachineName",
"WithProcessId",
"WithProcessName",
"WithThreadId"
],
"WriteTo": [
{
"Name": "Console",
"Args": {
"path": "Logs/logFile_.log"
},
"Theme": "Code"
},
{
"Name": "File",
"Args": {
"path": "Logs/logFile_.log",
"fileSizeLimitBytes": "1000",
"retainedFileCountLimit": 5,
"rollingInterval": "Day"
// , "shared" : "true"
},
"outputTemplate": "===> {Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}"
}
]
}
The filename looks like:
ProjectName.Api20220309_01.log
Rather than expected:
logfile_20220309_01.log
Is this something out of the box? Or is there something I should look for?
Just for background I have several projects in the one solution where serilog is running.

Can't get Serilog to write to a RollingFile log

I'm trying to use Serilog in a dotnet webapi app. I need logging to go to both the Console and a Rolling log file.
Everything is coming out to the Console but nothing is appearing in the rolling log file and I'm not getting any errors anywhere.
I have setup serilog in my code like so:
// in program.cs Main
var configuration = new ConfigurationBuilder()
.SetBasePath(BASEDIR)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext()
.Enrich.With(new ThreadIdEnricher())
.CreateLogger();
// startup.cs Configure
app.UseSerilogRequestLogging();
And in appsetting.json I have
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft.AspNetCore.Mvc.Internal": "Warning",
"Microsoft.AspNetCore.Authentication": "Warning",
"Microsoft.AspNetCore": "Warning",
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u1} TID:{ThreadId} {Message:lj}{NewLine}{Exception}",
"theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Literate, Serilog.Sinks.Console"
}
}
]
}
},
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "c:\\logs\\{Date}.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u1} TID:{ThreadId} {Message:lj}{NewLine}{Exception}",
"buffered": false,
"rollingInterval": "Serilog.RollingInterval.Day, Serilog.Sinks.File",
"retainedFileCountLimit": 7
}
}
]
}
}
]
}
}
have you tried to change to static file name? does the path exists? it seems that the configuration on the readme from their github page is a bit differnt from yours: https://github.com/serilog/serilog-sinks-file#user-content-json-appsettingsjson-configuration:~:text=node%2C%20%3A-,%7B,%7D,-See%20the%20XML
also maybe try to work your way back from setting it up in code then move back to config file?

Problems configuring Serilog from appSettings.json

I have a question relating the configuration of Serilog by appSettings.json.
My project is a console application, based on .Net Core 3.0.
I tried to setup Serilog in order to write log information to the console and to a log file.
When I configure everything within my code itself - everything works like expected.
But when I try to import the Serilog configuration settings from an appsettings.json file, I have the problem, that my ConsoleSink works fine, but my FileSink not.
Here is, what I wrote into my appsettings.json:
{
"Serilog": {
"Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Console" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "File",
"Args": { "pathFormat": "Xlog.txt" }
},
{ "Name": "Console" }
]
}
}
And this is the associated code:
IConfiguration configSerilog = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
.Build();
log = new LoggerConfiguration()
.ReadFrom.Configuration(configSerilog)
.CreateLogger();
log.Information("Initializing Serilog....");
My 'test-log-message' get's displayed in my console window, but I didn't get a logfile.
As I mentioned, it works, when I configure the Serilog in my code itself, rather than by the appsettings.
So, I do have the appropriate access rights to create a file.
Any ideas or hints?
Make sure that file proprieties for appsettings.json, property "Copy to Output Directory" is "Copy always" or "Copy if Newer".
And update youe configration as below (pathFormat in Args shoud be path)
{
"Serilog": {
"Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Console" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "File",
"Args": { "path": "Xlog.txt" }
},
{ "Name": "Console" }
]
}
}

serilog minimumlevel override not working for console but does for rolling file

I have the following code which successfully only logs Information logs that exclude Microsoft and other system ones, but the console still gets some info written to it which I would like to exclude
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": { "outputTemplate": "[{Timestamp:HH:mm:ss.fff}] {Level:u3} - {Message}{NewLine}{Exception}" }
},
{
"Name": "RollingFile",
"Args": {
"pathFormat": "log-{Date}.log",
"outputTemplate": "[{Timestamp:dd/MM/yy HH:mm:ss.fff z}] {Level:u3} {Message}{NewLine}{Exception}"
}
}
]
},
Also here is what gets written to console but not the rolling file based on above settings
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.0.1-rtm-125 initialized 'CryptoAlertContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (26ms) [Parameters=[#__p_0='?'], CommandType='Text', CommandTimeout='30']
SELECT TOP(#__p_0) [x].[CoinId], [x].[Icon], [x].[LastPriceBtc], [x].[LastPriceUsd], [x].[LastUpdated], [x].[Name], [x].[Rank], [x].[Symbol]
FROM [Coins] AS [x]
ORDER BY [x].[Rank]
It looks like you're missing UseSerilog() in your program start-up code: the console output you've show is from the default ASP.NET logging provider, not the Serilog one.

Using Microsoft.Extensions.Logging can you filter for a single EventID?

I'd like to produce a log file that contains only log output from a particular EventId (or EventIds). Is such functionality supported?
If you plug in Serilog as the provider, you can continue logging through Microsoft.Extensions.Logging but apply Serilog's filtering to limit what is sent to the log file.
To do that you'd use the following Serilog configuration:
Log.Logger = new LoggerConfiguration()
.Filter.ByIncludingOnly("EventId.Id = 9")
.WriteTo.RollingFile("logs/log-{Date}.txt")
.CreateLogger();
(Where 9 is whatever event id you want to include.)
You can plug in Serilog with https://github.com/serilog/serilog-aspnetcore, and to compile this example you'll also need to install the Serilog.Sinks.RollingFile and Serilog.Filters.Expressions packages.
If you want to config Serilog using the appsettings file you can use something like this:
"Serilog": {
"Using": ["Serilog.Sinks.File", "Serilog.Settings.Configuration", "Serilog.Expressions"],
"MinimumLevel": {
"Default": "Debug"
},
"WriteTo": [{
"Name": "File",
"Args": {
"path": "log.txt",
"rollingInterval": "Day",
"retainedFileCountLimit": "120",
"rollOnFileSizeLimit": "true",
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
}
}
],
"Filter": [{
"Name": "ByIncludingOnly",
"Args": {
"expression": "EventId.Id = 101"
}
}
]
}
In the using field you have to put all Serilog package that you are importing, then config a sink for writing the logs and finally define a filter.
I almost spent a whole morning trying to achive that so I hope it helps you

Categories

Resources