If you want to access your level switches from code, it probably means that you have a way to control them somehow, so you probably don't need them in the config file in the first place...
I do believe it makes more sense to keep that part entirely in the code and have the configuration partly in code and partly in the config file, so that would look like this :
// in C# code
var appLevelSwitch = new LoggingLevelSwitch(LogEventLevel.Debug);
var netLevelSwitch= new LoggingLevelSwitch(LogEventLevel.Information);
var systemLevelSwitch= new LoggingLevelSwitch(LogEventLevel.Error);
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
Log.Logger = new LoggerConfiguration()
// load config from config file ...
.ReadFrom.Configuration(configuration)
// ... and complete it in C# code
.MinimumLevel.ControlledBy(appLevelSwitch )
.MinimumLevel.Override("Microsoft", netLevelSwitch)
.MinimumLevel.Override("System", systemLevelSwitch)
.CreateLogger();
and in your config file
{
"Serilog": {
"Using": ["Serilog.Sinks.Console"],
"WriteTo": [
{ "Name": "Console" },
{ "Name": "File", "Args": { "path": "%TEMP%\\Logs\\serilog-configuration-sample.txt" } }
],
"Enrich": ["FromLogContext", "WithMachineName", "WithThreadId"],
"Destructure": [
{ "Name": "With", "Args": { "policy": "Sample.CustomPolicy, Sample" } },
{ "Name": "ToMaximumDepth", "Args": { "maximumDestructuringDepth": 4 } },
{ "Name": "ToMaximumStringLength", "Args": { "maximumStringLength": 100 } },
{ "Name": "ToMaximumCollectionCount", "Args": { "maximumCollectionCount": 10 } }
],
"Properties": {
"Application": "Sample"
}
}
}
For the sake of completeness, though, in order to access the defined control switches, you could do the following (be warned that this is kind of a hack !).
Write a configuration method (i.e. an extension method that can appear after .WriteTo.xxx
) that accepts LoggingLevelSwitch
es as arguments and stores them as static
members. That configuration method will introduce a dummy ILogEventSink
that does nothing (and for performance sake, we can even specify restrictedToMinimumLevel: LogEventLevel.Fatal
so that it is almost never called). Then invoke that extension method from the config file (Serilog.Settings.Configuration knows how to find extension methods and pass them the parameters) and voilà, you can now access the static
switches from your code !
Here is what it would look like :
public static class LevelSwitches
{
private static LoggingLevelSwitch _switch1;
private static LoggingLevelSwitch _switch2;
private static LoggingLevelSwitch _switch3;
public static LoggingLevelSwitch Switch1 => _switch1 ?? throw new InvalidOperationException("Switch1 not initialized !");
public static LoggingLevelSwitch Switch2 => _switch2 ?? throw new InvalidOperationException("Switch2 not initialized !");
public static LoggingLevelSwitch Switch3 => _switch3 ?? throw new InvalidOperationException("Switch3 not initialized !");
public static LoggerConfiguration CaptureSwitches(
this LoggerSinkConfiguration sinkConfig,
LoggingLevelSwitch switch1,
LoggingLevelSwitch switch2,
LoggingLevelSwitch switch3)
{
_switch1 = switch1;
_switch2 = switch2;
_switch3 = switch3;
return sinkConfig.Sink(
restrictedToMinimumLevel: LogEventLevel.Fatal,
logEventSink: new NullSink());
}
}
public sealed class NullSink : ILogEventSink
{
public void Emit(LogEvent logEvent)
{
// nothing here, that's a useles sink !
}
}
Then in you json config file :
"LevelSwitches": {
"$appLogLevel": "Debug",
"$netLogLevel": "Information",
"$sysLogLevel": "Error"
},
"MinimumLevel": {
"ControlledBy": "$appLogLevel",
"Override": {
"Microsoft": "$netLogLevel",
"System": "$sysLogLevel"
}
},
"WriteTo":[
{
"Name": CaptureSwitches"",
"Args": {
"switch1": "$appLogLevel",
"switch2": "$netLogLevel",
"switch3": "$sysLogLevel",
}
}
]
(you may need a "Using"
directive with the name of the assembly containing the LevelSwitches
class)
Configure your logger from the config file
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
From that point, you should be able to access the switches through LevelSwitches.Switch1
, LevelSwitches.Switch2
and LevelSwitches.Switch3
.