28

I was wondering what some of the best practices were around logging and logging frameworks and dependency injection. Specifically, if I am designing a class that needs a way to log, how should I go about getting an interface to log to keeping dependency injection in mind?

Dependency injection seems to state that external dependencies should be injected from the outside (constructor or property setters), so should I take an ILog instance in the constructor and use in the class? Should I consider logging an optional dependency and get it in a setter? Am I pushing for too much flexibility by allowing for the logging interface to change and should I just take a hard dependency on a specific logging interface (by e.g. create a static ILog variable via a call to a factory method)? Could this factory method call into the container to get the ILog implementation or will this create initialization conflicts between static variables being initialization and the IoC container being initialized?

Should I be doing this:

public class MyService : ISomeService
{
  private static readonly ILogger s_log = 
            LoggingFactory.GetLogger(typeof(MyService))
  ...
}

or perhaps this:

public class MyService : ISomeService
{
  protected virtual ILogger Logger {get; private set;}
  public MyService(ILogger logger, [other dependencies])
  {
    Logger = logger;
  }
}

or even this:

public class MyService : ISomeService
{
  public virtual ILogger Logger {get; set;}
  public MyService()
  {
  }
}

Other patterns or ways to do this? What are people out there doing? What's working and when?

1
  • I would opt for the second or third options depending on whether or not having a logger for the service is considered critical.
    – toad
    Feb 7, 2009 at 0:22

4 Answers 4

4

It's great that you're looking into inversion-of-control and dependency-injection.

But for your question, there's another concept you might want to look into: aspect-oriented programming.

In .NET, there are some good frameworks available for doing aspect-oriented programming, including Castle, LinFu, and Microsoft's Policy-Injection Application Block. In fact, some inversion-of-control containers have some aspect-oriented features in them as well.

These concepts and tools help make concerns such as logging take a background seat in terms of code noise, and make be handled automagically.

2
  • 1
    Logging is the "hello, world" of aspect-oriented programming. It's definitely a cross-cutting concern.
    – duffymo
    Feb 7, 2009 at 4:04
  • 6
    Handling method tracing and exceptions is one thing, but what about positive event logging? What about warning conditions you need to log when they happen -- but don't interrupt the actual program flow. Can you cover "all (relevant)" cases with aspects?
    – Cornelius
    Feb 12, 2013 at 18:15
4

My advice? Wrap the logging interfaces into your own. I took a dependency on Log4Net once, got burned and had to refactor a lot of my projects because of it.

2
  • @Karsten even if you inject the logger you still have to add a reference to every project that will use it so that the class or interface can be resolved. The solution of using a callback isolates 100% of the implementation to the scope where the class is created. Jan 19, 2017 at 21:39
3

This won't be a complete answer, but one consideration will be that if you inject your ILog via your class' constructor you can then mock that logging framework for your unit testing. Other thoughts... A property setter for passing in the ILog means that you can't log actions from the constructor. Also, you don't know for sure if your instance even has an ILog available meaning you have to wrap every call with a test for a valid ILog instance.

0
0

I would stick to injecting it and using an interface. Mainly to facilitate testing. Making it is easier to substitute a mock or stub when testing the consuming object.

I would base whether I used constructor or setter injection on the importance of the log to the consuming class. If you want it to be critical then I would favor constructor injection, if optional then setter.

I don't see where using a factory method couldn't work with the container, however, it then makes testing the consumer class dependent on a proper configuration of this factory method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.