1

I'm working on an ASP .NET Core 3.1 Web API project and encountering an issue with Serilog. The logs are written to the file only after the first request is made post-startup.

Here's a simplified version of my Program.cs

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureSerilog()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

The ConfigureSerilog method is defined in a static class:

    public static class SerilogConfig
    {
        public static IHostBuilder ConfigureSerilog(this IHostBuilder builder)
        {
            Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
            Directory.CreateDirectory("Logs");

            var configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.Serilog.json", optional: false, reloadOnChange: true)
                .Build();

            Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .Enrich.FromLogContext()
                .CreateLogger();

            builder.UseSerilog();

            return builder;
        }
    }

In my appsettings.Serilog.json, I've set shared to true since multiple IIS instances will log to the same file:

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "Using": [
      "Serilog.Sinks.File"
    ],
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "Path": "./Logs/log.log",
          "shared": "true",
          "RollingInterval": "Day",
          "RetainedFileCountLimit": null,
          "formatter": {
            "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
            "template": "[{@t:yyyy-MM-dd HH:mm:ss} {@l:u3}] {@m}\n{@x}"
          }
        }
      }
    ]
  }
}

How can I properly configure Serilog to ensure that every request from every IIS instance is logged to a single shared file without conflicts?

1
  • ideally you shouldn't write to the same file by multiple processes - merge after the fact! Commented Jun 3, 2024 at 18:23

1 Answer 1

0

You could consider using RabbitMQ to write to queue. This will cetainly avoid conflict. You could create an applcation to consume the queue and write to file one by one.
Write to rabbitmq: https://github.com/ArieGato/serilog-sinks-rabbitmq?tab=readme-ov-file#configuration-via-code
Read message from rabbitmq: https://www.rabbitmq.com/tutorials/tutorial-three-dotnet#putting-it-all-together

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.