I have some mvc controllers must instanciate a single logger according the fully qualified controller name.
I try to explain with examples. I have this logger interface:
public interface Ilogger { ... }
and its implementation
public class MyLogger
{
public MyLogger(loggerName)
{
...
}
...
}
Well, I need now different loggers per controllers.
namespace Controllers
{
public class MyFirstController
{
public MyFristController(Ilogger logger) { ... }
...
}
}
namespace Controllers
{
public class MySecondoController
{
public MySecondoController(Ilogger logger) { ... }
...
}
}
I need two different singletons... One per controller, where the constructor parameter loggerName
of MyLoggerClass
must change according the controller. So, I need two instances:
new MyLogger("Controllers.MyFirstController")
and
new MyLogger("Controllers.MySecondoController")
I am trying something like this:
builder.RegisterControllers(assemblies)
.PropertiesAutowired()
.WithConstructorParameter(typeof(ILoggerProvider));
Where WithConstructorParameter
is an extension method:
public static IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle> WithConstructorParameter<TLimit, TReflectionActivatorData, TStyle>(this IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle> registration, Type targetType)
where TReflectionActivatorData : ReflectionActivatorData
{
return registration.WithParameter(
(pi, ctx) => pi.ParameterType == targetType,
(pi, ctx) => ...)); // <-- I have been losing here....
}
Is it the correct approach? How can I solvemy problem?
Thank you
I have modified the extension methos as following:
public static IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle> WithNamedParameter<TLimit, TReflectionActivatorData, TStyle>(this IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle> registration, Type targetType)
where TReflectionActivatorData : ReflectionActivatorData
{
return registration.WithParameter(
(pi, ctx) => pi.ParameterType == targetType,
(pi, ctx) => ctx.ResolveNamed(pi.Member.DeclaringType.FullName, targetType)
);
}