Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog writes the logs twice

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.

like image 823
mohsen kamrani Avatar asked Jan 29 '19 02:01

mohsen kamrani


People also ask

Where does Serilog write to by default?

By default, serilog will only log to the console.

Can you stop logging with Serilog?

For example, if you are using Serilog you should configure Log. Logger and LibLog will use it. Also you can disable logging by setting LogProvider. IsDisabled to true . "

What is Serilog log context?

Serilog. Context. LogContext can be used to dynamically add and remove properties from the ambient "execution context"; for example, all messages written during a transaction might carry the id of that transaction, and so-on.

What is structured logging in Serilog?

Structured logging is a modern approach where logging events are treated as structured data rather than text, for example: { “payload”: { “order”: { “id”: 10 }, “message”: “OrderId: 10 placed successfully” }


1 Answers

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

like image 178
Jordy van Eijk Avatar answered Sep 19 '22 16:09

Jordy van Eijk