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!

3 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:
    log4net.Config.BasicConfigurator.Configure();

  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 http://logging.apache.org/log4net/release/manual/configuration.html#dot-config).
    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

Leave a Reply

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