Mysterious288
Mysterious288

Reputation: 447

C# Serilog conditional logging if one fails?

We are in the analysis stage, right now we have an architecture where Serilog will write to a local file, and then there is a Fluentd that will pull the logs and dumps them to the elastic search. The reason for such architecture is to make sure that there is no message loss. SUGGEST if this architecture is GOOD**???**

But recently I learned fluentd has the inbuilt buffer mechanism that stores the logs locally, so we thought of pushing the logs from the application directly to the fluentd instead of the local file, however, our concern is, if for some reason fluentd is down then the logs won't be processed further. So in this case can we tell Serilog at runtime to log the messages in the local file instead of fluentd if the fluentd is down, I have seen some conditional code in serilog but they are not at runtime, please help.

bool enableConsoleLogging = ... bool enableFileLogging = ...

var builder = new LoggerConfiguration()
    .Enrich.WithExceptionDetails()
    .Enrich.FromLogContext()
    .MinimumLevel.Warning()
    .WriteTo.Conditional(evt => enableConsoleLogging, wt => wt.Console())
    .WriteTo.Conditional(evt => enableFileLogging, wt => wt.File(...));

Log.Logger = builder.CreateLogger();

Upvotes: 0

Views: 1971

Answers (1)

C. Augusto Proiete
C. Augusto Proiete

Reputation: 27868

Serilog itself doesn't have a fallback mechanism built-in, so it's up to the sink to implement any kind of durable logging / retry mechanism.

If the Fluentd sink you're using does not have or implement a durable logging mechanism that meets your needs, and you want to take full control of the writes to Fluentd, you're better off writing your own Serilog Sink that sends messages to Fluentd and fallback to something else.

You might want to take a look at the source code of Serilog.Sinks.Http and this example with Fluentd.

Also, remember that writing to a file does not guarantee 100% you won't lose messages. There are many reasons why writing to disk can fail... Including running out of disk space.

ps: You might also be interested in the answers in this question here on SO: "Serilog and seq when no server available"

Upvotes: 2

Related Questions