0

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

Simone
  • 2,304
  • 6
  • 30
  • 79
  • [Have you read the docs? There's a great example almost exactly this.](https://autofaccn.readthedocs.io/en/latest/examples/log4net.html) – Travis Illig May 01 '20 at 04:53
  • Yes, I have seen that example... I have tryed to implement it but there was something I couldn't do... now I do not remember honestly because I have done different retries... I have solved with this line of code: `(pi, ctx) => ctx.ResolveNamed(pi.Member.DeclaringType.FullName, targetType)` – Simone May 01 '20 at 11:14
  • 1
    If you've solved it, you should post the answer to your own question so folks don't jump in here and continue trying to answer you. Glad you got it working! – Travis Illig May 01 '20 at 14:28

1 Answers1

0

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)
    );
}
Simone
  • 2,304
  • 6
  • 30
  • 79