1

So the way I usually do it (with DI) is to add the extension to my container:

unityContainer.AddNewExtension<Log4NetExtension>();

and then at the constructor of the class where I need to call the logger I use something like this:

public class test
{
    private ILog logger;
    public test (ILog logger)
    {
        this.logger =logger;
    }
}

Now my problem is, in one of my classes, I don't want to pass anything to the constructor and I was wondering how can I assign my logger (since im using unity I thought of calling resolve but it's not working)

public class test
{
    private ILog logger;

    public test()
    {
        logger = unityContainer.Resolve<ILog>(); //I edited this for simplicity
    }
}

Error is something like the container didn't know how to resolve ILog.

EDIT:

The class that didn't let me pass anything through its constructor is a Job (implements IJob) class, in the end I ended up using a job listener instead of logging in each job.

Some tips if you still want to pass something for the constructor is by implementation a Job Factory which should help you inject parameters. And I saw a nuget you can add to help you with Quartz integration with Unity.

Ali_Nass
  • 838
  • 1
  • 9
  • 27
  • Have you checked the order for registration and resolving? Are `unityContainer` and `DIContainer.Container` references the same instance of UnityContainer? – stukselbax Feb 15 '18 at 13:02
  • @stukselbax yeah my bad I should've mentioned that my DIContainer has a Container field which is the unitycontainer so it is the same. – Ali_Nass Feb 15 '18 at 13:14
  • How does `Log4NetExtension` looks like? could you provide code reproducing your problem? – stukselbax Feb 15 '18 at 13:22
  • The Log4NetExtension is a built-in extension from Unity and it usually works like in my first example(where I pass it through the constructor) – Ali_Nass Feb 15 '18 at 13:32

2 Answers2

1

Log4net allows you create a logger using a name in your class, no need to pass it via a constructor.

Add this as a class member

private static readonly log4net.ILog logger = log4net.LogManager.GetLogger("MyAppLog");

Of course, this does not use your DIContainer.

Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
  • 1
    Exactly, doing it your way will work for sure, but I want to do it using DI so I don't use the same thing in every class. – Ali_Nass Feb 15 '18 at 13:13
0

You can use property injection:

public class test
{
    [Dependency]
    public ILog Logger { get; set; }

    public test()
    {
    }
}

Two drawbacks, though:

  1. the logger's a public writable property now, so anyone could technically use or overwrite your logger
  2. property injection is meant for optional dependencies, so you might be confusing others who look at the class
Haukinger
  • 10,420
  • 2
  • 15
  • 28
  • Ah thanks for mentioning this I will keep it in mind! But for my case personally I ended up not using the logger in that class anyway. – Ali_Nass Feb 16 '18 at 10:36