Post

Injected log4net Logger

Blog post is for personal reference for the cases when you need to get instance of some sort of logger from the Logging library you are using.

It depends on logging library that you use but usually you may get instance of logger itself by providing some metadata about calling site or producer of the log entries.

Code usually looks somewhat similar:

1
private readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

As you can see it’s not really ready for injection as it requires concrete type in order to initialize an instance. I would expect to have possibility to wait for somebody to give me correct logger already configured with concrete producer of the log entries.

1
2
3
4
5
6
public class MyController : Controller
{
    public MyController(ILog logger)
    {
    }
}

Instruct DependencyResolver to create a logger

It depends on type of IoC framework you are using but majority of main players in that field provide a way to initialize and customize creation of a container. Code snippet below uses StructureMap library.

1
2
3
4
5
6
7
8
public class StructureMapRegistry : Registry
{
    public StructureMapRegistry()
    {
        For().AlwaysUnique().Use(ctx =>
            LogManager.GetLogger(ctx.ParentType ?? ctx.BuildStack.Current.ConcreteType));
    }
}

So whenever somebody will ask for ILog interface from IoC container it will be constructed with proper producer type.

Happy logging!

[eof]

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.