In my .NET Core 3.1 console application, I am reading a large text file with records sequentially and logging the record number and other information using the below Serilog sinks:
"Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Async"
When I checked the log file, log file doesn't contains the all logging data (i.e. any record numbers) are missing in the log file.
I used the below configuration to configure the Serilog using appSettings.json file.
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Async" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level:u4}] {Message:lj} [{SourceContext}] {Exception}{NewLine}"
}
},
{
"Name": "File",
"Args": {
"path": "C:\\FeedReader\\Logs\\AppLog-.log",
"rollOnFileSizeLimit": "true",
"rollingInterval": "Hour",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level:u4}] {Message:lj} [{SourceContext}] {Exception}{NewLine}",
"fileSizeLimitBytes": 1048576, // 10 MB
"retainedFileCountLimit": 999,
"shared": false, // Turn off file sharing for better performance
"buffered": true, // Use buffered writes for better performance
"flushToDiskInterval": "00:00:05" // Flush every 5 seconds
}
}
]
}
}
]
}
The Program.cs code that initializes the logger and configuration:
var baseDir = AppDomain.CurrentDomain.BaseDirectory;
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
if (string.IsNullOrEmpty(environmentName) == false)
{
builder.AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
Log.Information("===================================Application is starting=================================");
This is the snapshot of one log file generated:
I kept the Program.cs code that initializes the logger and configuration same and tried to use the simple Serilog File Sink without Async sink, logs are generating in sequence and logs are not missing. I use the below configuration for the File Sink.
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\FeedReader\\Logs\\AppLog.log",
"fileSizeLimitBytes": 1048576,
"rollingInterval": "Hour",
"rollOnFileSizeLimit": "true",
"retainedFileCountLimit": 999,
"flushToDiskInterval": "00:00:05",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level:u4}] {Message:lj} [{SourceContext}] {Exception}{NewLine}"
}
},
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level:u4}] {Message:lj} [{SourceContext}] {Exception}{NewLine}"
}
}
]
}

