Is there any way I can get the context so I can retrieve the loggerName and use LogManager.GetLogger(loggerName) instead of LogManager.GetCurrentClassLogger()?
I noticed container.RegisterConditional() has access to the context.
Also, I want to avoid solutions like SimpleLogging.NLog for now.
Finally, I am willing to accept this is not the right approach. BTW, AOP is an option that I've already explored (Is it a good practice to have logger as a singleton?).
Note: I am aware that GetCurrentClassLogger() gets the same information I'd retrieve with .NET reflection.
using NLog;
using SimpleInjector;
namespace DependencyInjection
{
class Program
{
private static Container _container;
static void Main(string[] args)
{
Bootstrap();
_container.GetInstance<Greeter>().Greet();
}
private static void Bootstrap()
{
_container = new Container();
_container.Register<ILogger>(() => LogManager.GetCurrentClassLogger(), Lifestyle.Transient);
_container.Register<Greeter>();
_container.Verify();
}
public class Greeter
{
private ILogger _logger;
public Greeter(ILogger logger)
{
_logger = logger;
}
public void Greet()
{
_logger.Log(LogLevel.Info, "Hello world!");
}
}
}
}