RoBlog – Refactoring Reality

Discrete thought packets on .Net, software development and the universe; transmitted by Rob Levine

Using log4net with Unity (part 2) – UnityLog4NetExtension now on NuGet

by Rob Levine on 21-Dec-2013

Following on from a previous post, the solution detailed has now been renamed to “UnityLog4NetExtension” and is now available as a NuGet package as well as via GitHub.

So if you want to dependency inject the correct log4net ILog logger instance for your type, all you need to do is:

1) reference the NuGet package
2) add the extension to your unity IContainer instance with

1
container.AddNewExtension<Log4NetExtension>();

3) and add your ILog dependencies wherever you need a log4net logger:

1
2
3
4
5
6
7
8
9
public class MyClass
{
    private readonly ILog _logger;
 
    public MyClass(ILog loggerForMyClass)
    {
        _logger = loggerForMyClass;
    }
}

Enjoy!

Leave a Reply to Anonymous Cancel reply

Your email address will not be published. Required fields are marked *