How do you derive a context class using log4net as a service?

I use Log4Net as a service that is injected into other services using StructureMap.

How to ensure that the log file includes the class context of the calling service (class name and / or stream) that calls log4net calls?

Of course, the calling class or thread will always be a logging service that does not help me understand where the logging calls actually come from.

EDIT:

Registration Code:

  ObjectFactory.Initialize(x =>
    {           
        x.For<ILog>().AlwaysUnique().Use(s => s.ParentType == null ? 
            LogManager.GetLogger(s.BuildStack.Current.ConcreteType) : 
            LogManager.GetLogger(s.ParentType));

    });

Service Level:

public class LoggerService : ILoggerService
    {
        private readonly ILog log;

        public LoggerService(ILog logger)
        {            
            log = logger;
            log.Info("Logger started {0}".With(logger.Logger.Name));
        }       

        public void Info(string message)
        {
            log.Info(message);
        }
}

In logging, I still always get the LoggerService as a context, so I will never see what is actually called a registrar. It seems to be working incorrectly. I feel that something is missing here ...

Edit 2: I added a paste link for the console application:

http://pastie.org/1897389

, , .

+2
2

, . Google Group.

Ayende AOP Log4Net Windsor.

0

StructureMap , , StructureMap, , .

StructureMap 2.6.2, 2.5+, .For < > (). < > ().

public class CommonsRegistry : Registry
{
  public CommonsRegistry()
  {
    For<ILogger>().AlwaysUnique().Use(s => s.ParentType == null ? new Log4NetLogger(s.BuildStack.Current.ConcreteType) : new Log4NetLogger(s.ParentType.UnderlyingSystemType.Name));
    XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(Path.GetDirectoryName(Assembly.GetAssembly(GetType()).Location), "Log.config")));
  }
}

, ILogger , , , - , /context.

* , (XmlConfigurator.ConfigureAndWatch) Log4Net, "Log.config" , , .

, , IOC.Startup, , .

ObjectFactory.Initialize(x =>
{
  x.AddRegistry<CommonsRegistry>();
  ...
}

, , , , - .

class foo
{
  private readonly ILogger _log;

  public foo(ILogger log)
  {
    _log = log;
  }
}

/ "foo".

0

All Articles