如何使用log4net作为服务输出上下文类? [英] How do you output the context class using log4net as a service?

查看:118
本文介绍了如何使用log4net作为服务输出上下文类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Log4Net作为服务,并使用StructureMap将其注入到其他服务中.

I am using Log4Net as a service which is injected into other services using StructureMap.

如何确保日志文件包含进行log4net调用的调用服务类上下文(类名称和/或线程)?

How do I ensure the log file includes the calling service class context (class name and/or thread) which is making the log4net calls?

当然,调用类或线程将始终是日志记录服务,这并不能帮助我了解日志记录调用的真正来源.

Surely the calling class or thread will always be the logging service which doesn't help me understand where the logging calls are really coming from.

注册码:

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

    });

服务层:

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);
        }
}

在日志记录中,我仍然始终将LoggerService作为上下文,因此我永远也看不到实际称为logger的内容.它似乎无法正常工作.我觉得我在这里想念什么...

In the logging, I am still always getting the LoggerService as the context so I'll never see what actually called the logger. It doesn't seem to be working correctly. I feel like I'm missing something here...

我在此处为控制台应用程序添加了粘贴链接:

Edit 2: I've added a pastie link for a console app here:

http://pastie.org/1897389

我希望可以记录父类,但是它不能在最简单的级别上工作.

I would expect the parent class to be logged but it isn't working at the simplest of levels.

推荐答案

我在生成的许多代码中都使用了StructureMap,并且我有一个StructureMap注册表,可用于将记录器连接到该类的上下文中注入.

I use StructureMap in a lot of the code I generate and I have a StructureMap registry which I use to hook the logger into the context of the class that it is injected into.

作为参考,我使用的是Structure6.2的2.6.2版本,但在使用新的.For<>().Use<>()格式的2.5+上应该可以.

For Reference, I'm using the 2.6.2 version of StructureMap but should be fine with 2.5+ where the new .For<>().Use<>() format is utilized.

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的任何地方,使用注入它的类是记录日志消息的位置/上下文.

What this registry is doing is for anywhere the ILogger is injected, use the class that it's injected into is where the logging messages are logged to/context of.

*此外,在第二行(XmlConfigurator.ConfigureAndWatch)中,我告诉Log4Net从文件"Log.config"而不是应用程序配置文件中获取日志记录信息,您可能会喜欢,也可能不喜欢省略.

*Also, in the second line (XmlConfigurator.ConfigureAndWatch) is where I tell Log4Net to get the logging information from the file "Log.config" instead of the application configuration file, you may or may not like that and can be omitted.

我使用的代码是一个常见的IOC.Startup例程,如果我想使用默认的registery,我将在其中传递该代码.

The code I use is a common IOC.Startup routine where I would pass if I would like to use the default registery.

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

这为我提供了在日志记录实例中的调用类名称,在该实例中,消息将自动记录到日志中,而所需要做的只是将记录器注入到该类中.

This gives me the calling class name in the logging instance where messages are logged to automatically and all that is required is to inject the logger into the class.

class foo
{
  private readonly ILogger _log;

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

现在,消息被记录为上下文/类"foo".

Now the messages are logged as context/class "foo".

这篇关于如何使用log4net作为服务输出上下文类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆