0

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:

Log Screenshot

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}"
        }
      }
    ]
}

1 Answer 1

0

After reading the serilog-sinks-async documentation, I found a configuration "blockWhenFull". Sinks like File etc. keep the log in the queue and if the queue is full sinks start rejecting the logs. The logs are rejected to avoid the blocking of the main thread.

If you don't want to reject the log use "blockWhenFull": true;

Serilog Sinks Async Configuration

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.