I'm configuring logging for my .NET Core application (WPF and .NET Core 3). I'd like to leverage Microsoft.Extensions.Logging
through my libraries so there is a common interface and I can easily swap out the logging provider when configuring DI for the application. Reading this SO question and this blog post it appears to be a bad idea to inject either ILoggerFactory
or ILogger<T>
into consuming classes. Instead we ought to be injecting ILogger
but this will require a third-party IOC container to do that for us.
Based on this, and since we're using Ninject, I've configured the Ninject DI as follows:
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
kernel.Bind<ILogger>()
.ToMethod(context =>
{
var categoryName = context?.Request?.ParentRequest?.Service.FullName ?? "Unspecified";
return loggerFactory.CreateLogger(categoryName);
});
which I think should give me the same behaviour as using ILogger<T>
but using ILogger
. Given that I've found this logging stuff somewhat confusing, does the above seem reasonable or am I doing something glaringly wrong?