It is possible using Autofac.
If you use only the MS logging then you can create an Autofac module that will resolve the appropriate MS logger type for you.
using Autofac;
using Autofac.Core;
using Autofac.Core.Registration;
using Autofac.Core.Resolving.Pipeline;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
public class DependencyModule : Module
{
protected override void AttachToComponentRegistration(IComponentRegistryBuilder componentRegistry, IComponentRegistration registration)
{
registration.PipelineBuilding +=
(sender, e) =>
{
e.Use(
"ICustomLogger instance injection middleware",
PipelinePhase.ParameterSelection,
MiddlewareInsertionMode.StartOfPhase,
(context, next) =>
{
var parameters = context.Parameters.ToList();
parameters.Add(
new ResolvedParameter(
(parameter, context) => typeof(ICustomLogger) == parameter.ParameterType,
(parameter, context) =>
{
// This is the type the ctor of which contains the ICustomLogger parameter.
var type = parameter.Member.DeclaringType;
// This is the Microsoft.Extensions.Logging.ILogger<T> instance.
var msLogger = context.Resolve<ILoggerFactory>().CreateLogger(type);
// Here we create an instance of ICustomLogger.
return Activator.CreateInstance(typeof(MicrosoftLogger<>).MakeGenericType(type), msLogger)
}
)
);
context.ChangeParameters(parameters);
next.Invoke(context);
}
);
};
}
}
Then you need to register this module into Autofac container, e.g. when you create the hostbuilder for your app.
using Autofac;
using Autofac.Extensions.DependencyInjection;
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(
builder => builder.RegisterModule<DependencyModule>()
)
// rest is ommitted for clarity
;
Then your consumer class will get the correct instance of your MicrosoftLogger<T>
type.