Serilog : choose which sink to log at runtime
Asked Answered
L

1

12

I'm going to implement Serilog within .net standard 2.0 library. I'm looking for a way to choose which sink(s) should be used for each log line.

Lets say that we define 2 sinks within the configuration (console & file) :

    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Debug()
        .WriteTo.Console()
        .WriteTo.File("c:\\temp\\SerilogTets.txt")
        .CreateLogger();

After this, we are going to write one log rule :

Log.Information("Hello, world!");  //<- how can we define which sink we want to use

I'm looking for a way howto define for which sink(s) those lines should be logged for :

  • Console & File
  • Console only
  • File only

Without relying on what loglevel it is.

Thank you in advance!

Kind regards, Kurt

Leary answered 8/1, 2021 at 11:40 Comment(5)
You do that by specifying filter conditions in the configuration. The application code shouldn't know anything about the sinks. That defeats the purpose of using a logging library to begin with - decouple application code from logging sinks.Valediction
Besides, one can't tell you how to select a sink if you don't explain what the criteria are for selecting one sink or another.Valediction
What @PanagiotisKanavos says, see #46516859Opalina
Thanks for your quick response. The main reason is : There is data that we do want to log only to log files because of security reasons. Other log information may be logged to application insights. It's more to want to have a choice what we want to push to application insights and what not.Leary
That's what filtering in the logger configuration does. You can specify filters by category and extra context propertiesValediction
D
15

In Serilog you can do this separation via sub-loggers using filters or via Serilog.Sinks.Map, using context properties to decide which logger will include or exclude certain messages.

The example below defines that all log events will be written to both the Console and to a File, by default, however if the log event has a property called FileOnly in the log context, it will not be written to the Console, and likewise if the log event has a property called ConsoleOnly, it will not be written to the File.

using Serilog;
using Serilog.Context;
using Serilog.Filters;
// ...

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .Enrich.FromLogContext()
    .WriteTo.Logger(c =>
        c.Filter.ByExcluding(Matching.WithProperty("FileOnly"))
            .WriteTo.Console())
    .WriteTo.Logger(c =>
        c.Filter.ByExcluding(Matching.WithProperty("ConsoleOnly"))
            .WriteTo.File("C:\\Temp\\SerilogTests.txt"))
    .CreateLogger();

// Writes to both Console & File
Log.Information("Hello, world! (Console and File)");

using (LogContext.PushProperty("ConsoleOnly", value: true))
{
    // Writes only to the Console
    Log.Information("Hello, world! (Console)");
}

using (LogContext.PushProperty("FileOnly", value: true))
{
    // Writes only to the File
    Log.Information("Hello, world! (File Only)");
}

Log.CloseAndFlush();

N.B.: Ideally you'll come up with better property names that are more generic, so that when you are writing logs within your application it doesn't have to know anything about "Console" or "File". e.g. You could have a property called Secret or Classified and decide where to write the logs based on the presence of this property or not.

There are different ways to add properties to a log event, including adding the property in the message template directly when you Log.Information, etc., via LogContext.PushProperty as in the example above, and vi Log.ForContext.

You can see other examples of filtering, sub-loggers, and Serilog.Sinks.Map here:

Damson answered 8/1, 2021 at 16:41 Comment(2)
Thanks for providing your answer. I did found this one before, but the problem i have with that one is that it will also log the (console) & (File only) within the log file.Leary
@KurtColemonts I'm not sure I understand what you mean. Do you want to update your question with more details?Damson

© 2022 - 2024 — McMap. All rights reserved.