I have written a plugin architecture in .net core 3.1. The main program loads the plugins and establishes DI bindings for code within those plugins. The main program will obtain a plugin object reference, with DI, and then execute a method on that object. When it fetches that plugin object via DI, I want the ILogger
which has been injected into that object reference (and sub-dependencies) to already have the name of the plugin configured (i.e. the assembly name of the plugin).
So for example, I want code within the plugin to do this:
// Assume Serilog injection provided by SimpleInjector
ILogger logger;
// No need to specify the parameter value for 'Plugin', if this is done correctly!
logger.Information("This is the plugin called {Plugin}");
The normal way to configure an ILogger instance for the above behavior, is with code like this:
logger = log.ForContext("Plugin", "CoolPlugin.DLL");
It is possible to create a factory-like binding in SimpleInjector, but unfortunately that factory has no knowledge of the type into which the new dependency is being injected:
container.Register<ILogger>(() => log);
I've also looked at Serilog enrichers, but they likewise do not have access to the type information from the class from which they were called. Finally, the using-context construct of Serilog only works at the precise moment of construction, and is thereafter unavailable. At least this is true for me, wherein all my code is async.
At this point I don't think this is an achievable goal using my two chosen technologies (Serilog and SimpleInjector). I'd like to know if anyone has a potential solution.