如何按命名空间对log4net中的日志消息进行分组?

时间:2011-03-28 10:13:48

标签: c# log4net ioc-container

我正在尝试实现自己的log4net appender,它会将按名称空间的消息分组。 问题是,我不能像我想的那样让log4net记录LocationInformation。

  class Program
{
    protected static IWindsorContainer container;
    static void Main(string[] args)
    {
        container = new WindsorContainer();
        container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));

        // add facilities
        container.AddFacility("logging.facility", new LoggingFacility(LoggerImplementation.Log4net, "log4net.config"));
        container.Register(Component.For<ParentClass>().ImplementedBy<ParentClass>());
        container.Register(Component.For<ChildClass>().ImplementedBy<ChildClass>());                      

        var parentClass = container.Resolve<ParentClass>();
        var childClass = container.Resolve<ChildClass>();
        parentClass.Init();
        childClass.Init();

    }
}

public class ParentClass
{
    private readonly ILogger logger;

    private ILog localLogger;

    public ParentClass(ILogger logger)
    {
        this.logger = logger;
        localLogger = LogManager.GetLogger(GetType());
        logger.Info("ctor ioc logger, type: {0}",GetType().Name);
        localLogger.Info(string.Format("ctor local logger, type: {0}", GetType().Name));
    }

    public void Init()
    {
        logger.Info("init ioc logger, type: {0}", GetType().Name);
        localLogger.Info(string.Format("init local logger, type: {0}", GetType().Name));
    }
}

public class ChildClass : ParentClass
{
    public ChildClass(ILogger logger) : base(logger)
    {

    }
}

输出

2011-03-28 09:58:55 INFO 10 ParentClass - ctor ioc logger, type: ParentClass
2011-03-28 09:58:55 INFO 10 ParentClass - ctor local logger, type: ParentClass
2011-03-28 09:58:55 INFO 10 ParentClass - ctor ioc logger, type: ChildClass
2011-03-28 09:58:55 INFO 10 ParentClass - ctor local logger, type: ChildClass
2011-03-28 09:58:55 INFO 10 ParentClass - init ioc logger, type: ParentClass
2011-03-28 09:58:55 INFO 10 ParentClass - init local logger, type: ParentClass
2011-03-28 09:58:55 INFO 10 ParentClass - init ioc logger, type: ChildClass
2011-03-28 09:58:55 INFO 10 ParentClass - init local logger, type: ChildClass

问题是,log4net类名始终作为基类名输出(ParentClass)。我最初认为,这可能是城堡windsor,这导致了这样的问题,但结果显示,即使我手动创建记录器实例,它仍然记录基类名称。可能有人有想法,如何强制log4net写实际的实例类名?

更新

这里使用了log4net配置:   

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  <file value="c:\temp\testlog.log" />
  <appendToFile value="true" />
  <maximumFileSize value="10MB" />
  <maxSizeRollBackups value="2" />

  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss} %level %thread %C{1} - %message%newline" />
  </layout>
</appender>

<root>
  <level value="INFO" />
  <appender-ref ref="RollingFile" />      
</root>

以上输出是我得到的,我期望获得的是:

2011-03-28 09:58:55 INFO 10 ParentClass - ctor ioc logger, type: ParentClass
2011-03-28 09:58:55 INFO 10 ParentClass - ctor local logger, type: ParentClass
2011-03-28 09:58:55 INFO 10 ChildClass - ctor ioc logger, type: ChildClass
2011-03-28 09:58:55 INFO 10 ChildClass - ctor local logger, type: ChildClass
2011-03-28 09:58:55 INFO 10 ParentClass - init ioc logger, type: ParentClass
2011-03-28 09:58:55 INFO 10 ParentClass - init local logger, type: ParentClass
2011-03-28 09:58:55 INFO 10 ChildClass - init ioc logger, type: ChildClass
2011-03-28 09:58:55 INFO 10 ChildClass - init local logger, type: ChildClass

更新2

如果有人需要这样的话,这里有工作的appender实现:

public class GroupStringListAppender : log4net.Appender.AppenderSkeleton
{
    private readonly int listLength = 30;
    private readonly Func<string, string> pathFuction;
    private readonly Dictionary<string, List<string>> messagesDictionary = new Dictionary<string, List<string>>();

    public GroupStringListAppender(int listLength, Func<string, string> pathFuction)
    {
        this.listLength = listLength;
        this.pathFuction = pathFuction;
    }

    public List<string> GetMessages(string path)
    {
        return messagesDictionary[path];
    }        

    protected override void Append(LoggingEvent loggingEvent)
    {
        var msg = loggingEvent.RenderedMessage;
        var path = pathFuction.Invoke(loggingEvent.LoggerName);

        if (!messagesDictionary.ContainsKey(path))
        {
            messagesDictionary.Add(path, new List<string>());
            OnNewPathCreated(path);
        }

        var list = messagesDictionary[path];        

        while (list.Count - listLength > 0)
        {
            list.RemoveAt(0);    
        }

        list.Add(msg);

        OnAppended(path, list);          
    }

    public event AppendedEventHandler Appended;

    public void OnAppended(string path, List<string> list)
    {
        AppendedEventHandler handler = Appended;
        if (handler != null) handler(this, new LoggerMessageAppendedEventArgs(path, list));
    }

    public event NewPathCreatedHandler NewPathCreated;

    public void OnNewPathCreated(string path)
    {
        var handler = NewPathCreated;
        if (handler != null) handler(this, new NewPathCreatedHandlerArgs(path));
    }
}

public delegate void NewPathCreatedHandler(object sender, NewPathCreatedHandlerArgs args);

public class NewPathCreatedHandlerArgs : EventArgs
{
    public string Path { get; set; }

    public NewPathCreatedHandlerArgs(string path)
    {
        Path = path;
    }
}

public class LoggerMessageAppendedEventArgs : EventArgs
{
    public string Path { get; set; }
    public List<string> List { get; set; }

    public LoggerMessageAppendedEventArgs(string path, List<string> list)
    {
        Path = path;
        List = list;
    }
}

public delegate void AppendedEventHandler(object sender, LoggerMessageAppendedEventArgs e);

1 个答案:

答案 0 :(得分:0)

使用%logger代替%C{1},您至少会为本地记录器获得所需的结果。

我认为对于IoC,你需要多做一点。必须对此进行更多调查...