10

I'm using Serilog with Elasticsearch sink with the configurations like this:

Log.Logger = new LoggerConfiguration()
                    .MinimumLevel.Verbose()
                    .MinimumLevel.Override("Microsoft", LogEventLevel.Verbose)
                    .Enrich.FromLogContext()
                    .Enrich.WithExceptionDetails()
                    .Enrich.WithProperty("Application", "abc")
                    .Enrich.WithProperty("Environment", env.EnvironmentName)
                    .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(Configuration["LoggingEndpoint"]))
                    {
                        AutoRegisterTemplate = true,
                        CustomFormatter = new ExceptionAsObjectJsonFormatter(renderMessage: true) // Better formatting for exceptions
                    })

// and later:

services.AddLogging(loggingBuilder =>
            loggingBuilder.AddSerilog());

But I can see every log twice, with a couple of milliseconds difference in their timestamp, on the Kibana. I tried the solutions provided here, just in case they might help, but no luck.

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66

1 Answers1

12

This is probably the case because you are configuring Serilog inside your appsettings.json and also inside code. This wil Log everything twice.

Jordy van Eijk
  • 2,718
  • 2
  • 19
  • 37
  • Thank you! That was exactly the case in my code. I was using WriteTo(new JsonFormatter()) and right after it ReadFrom.Configuration. I removed the latter and now it logs properly. – virouz98 Oct 20 '21 at 10:49