1

I'm using a JSON config file with overrides in environment variables. This file includes Serilog configuration, and I'd like to be able to disable a specific sink except under specific conditions using an environment variable override.

I've found that setting environment variables like in this Stack Overflow answer to override array elements of of WriteTo with a string will effectively disable that sink. For example, I can set the environment variable Serilog:WriteTo:1 to the string disable (or any other string). However, using a named property would make more sense, e.g. Serilog:WriteTo:SpecialCaseSink.

Looking at the source code for ConfigurationReader (below), it looks like this would work, as it only depends on call GetChildren to iterate over the WriteTo section, and that's what gets used by ConfigurationLoggerConfigurationExtensions.

Is there any drawback to this named approach other than the risk of Serilog changing its internal implementation? Is there a different way to selectively enable or disable a Serilog sink that doesn't use indices using just configuration?

Thanks!

void ApplySinks(LoggerConfiguration loggerConfiguration)
{
    var writeToDirective = _section.GetSection("WriteTo");
    if (writeToDirective.GetChildren().Any())
    {
        var methodCalls = GetMethodCalls(writeToDirective);
        CallConfigurationMethods(methodCalls, FindSinkConfigurationMethods(_configurationAssemblies), loggerConfiguration.WriteTo);
    }
}

and later

internal ILookup<string, Dictionary<string, IConfigurationArgumentValue>> GetMethodCalls(IConfigurationSection directive)
{
    var children = directive.GetChildren().ToList();

    var result =
        (from child in children
            // big query
        );

    return result;
}
Becca Dee
  • 1,530
  • 1
  • 24
  • 51

0 Answers0