views:

591

answers:

1

Hi

I have a static class which calls a static Logger class,

e.g

static class DoesStuffStatic
{
  public static void DoStuff()
  {
    try
    {
      //something
    }
    catch(Exception e)
    {
      //do stuff; 
      Logger.Log(e);
    }
  }
}

static class Logger
{
  public static void Log(Exception e)
  {
     //do stuff here
  }
}

How do I inject the Logger into my static class?

Note: I've read http://stackoverflow.com/questions/743951/help-with-dependency-injection-in-net, but this seems to use an instance logger.

+5  A: 

You can't inject a static logger. You have to either change it to an instance logger (if you can), or wrap it in an instance logger (that will call the static). Also it is fairly hard to inject anything to a static class (because you don't control the static constructor in any way) - that's why I tend to pass all the objects I want to inject as parameters.

Grzenio