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


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

public class MyClass
    private readonly ILog _logger;
    public MyClass(ILog loggerForMyClass)
        _logger = loggerForMyClass;


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

  1. Kris Adams says:

    Hi there. Thanks for this extension. It is awesome.

    I couldn’t get it working out of the box easily like you mentioned. It wouldn’t throw any errors, but just wouldn’t log. I am not sure if it was specific to my implementation or if you forgot to mention these things, but I thought I would come and post my findings.

    I have an App.Config, which contains an Appender, that was never used.

    In order to fix the issue, I had to add the following line somewhere below using statements:
    [assembly: log4net.Config.XmlConfigurator(Watch = true)]

    And then when doing your extension, I need to add this line as well:

  2. Rob Levine says:

    Hi Kris,

    There are a few gotchas here (all are related to log4net itself, and not this extension):

    1) You can’t use the XmlConfigurator assembly attribute if your log4net config is in the app.config/web.config. (see
    This only works if your log4net is in an external file (this can be very useful though, because by setting Watch=true then log4net will monitor this file for changes and let you reconfigure your logging at runtime without a restart). If using app.config/web.config you need to configure in code (as you have done)

    2) The BasicConfigurator is the wrong configurator to use – this is intended for use where you are building up the entire configuration in code as opposed to xml. I suspect the reason it seems to work here, is because of the combination of it and the XmlConfigurator attribute.

    You should be able to replace both the attribute and the BasicConfigurator call with a single call to XmlConfigurator.Configure();

  3. Cyrus Curwood says:

    Ah, I have just tested this with the EventLogAppender and it does work correctly if I have the XmlConfigurator.Configure(); in the factory class.

    Events are logged as they happen into the Event Log

    It appears that it is something specific tothe FileAppender which waits for IIS

  4. Stefan says:

    Any chance of getting the extension to work with Resolve() also? As it is it only works with properties marked with the [Dependency] attribute, but not if resolving through the container.

  5. Thiru says:

    I am trying to use ILog in different project(data access layer) but it is throwing exception dependency not resolved. I have a web api project and data access layer project. it is working fine for the controller level in web api project.

Leave a Reply

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