I know that this is something one should usually NOT do but there are situations where it is required. Here is one I currently have:
I use Serilog as my logger. My services run in docker containers which may or maynot be orchestrated in a datacenter environment. I use a Graylog
server to store log events from all my servers in a central place for easy monitoring and searching. The sysops in the datacenter need to be able to change the loglevel at runtime without restarting the services ideally from the command line in a bash shell using a script or curl
.
My solution is a REST API which allows to change the log-level.
Serilog offers an object LoggingLevelSwitch
to do that. In C# this looks as follows:
var logLevelSwitch = new LoggingLevelSwitch
{
MinimumLevel = LogEventLevel.Information
};
var loggerConfig = new LoggerConfiguration()
.MinimumLevel.ControlledBy(logLevelSwitch) //<-- this allows dynamic changes later in the code
.WriteTo.Graylog(new GraylogSinkOptions
{
HostnameOrAddress = po.LogHost,
TransportType = TransportType.Udp,
Port = 12201,
Facility = "BizBus",
});
Problem
I configure the logger in my Main()
function in Program.cs
long BEFORE the IOC container is initialized in AppHost.Configure()
. This is why I would like to add the already existing instance of LoggingLevelSwitch
to Func as soon as it is initialized, typically in the Main()
function I would do:
var wHost = BuildWebHost(args, po);
var iocContainer = HostContext.Container;
// register instance of previously created LoggingLevelSwitch object something like
// iocContainer.Register(myLoggingLevelSwitch instance);
wHost.Run();
Is this somehow possible with the Func IOC container or do I need to create my own (properly serialized) singleton to achieve this?