问题描述
我正在寻找一种将所有附加程序附加到一个记录器实例的方法.
I'm looking for a way to get all appenders attached to one logger instance.
我尝试过:
Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy;
hierarchy.GetAppenders()
根据文档,这将返回当前配置的所有记录器的所有附加器.
as per documentation this returns all appenders for all loggers currently configured.
当我尝试此操作时:
LogManager.GetLogger("MyLoggerName").Logger.Repository.GetAppenders();
我得到相同的结果.
我只想检索附加到一个记录器的附加器(在这种情况下为"MyLoggerName")
I would like to retrieve only appenders attached to one logger ("MyLoggerName" in this case)
我在哪里错了?
推荐答案
调用以下代码时
LogManager.GetLogger("MyLoggerName").Logger.Repository.GetAppenders();
实际上,您要查询与hierarchy.GetAppenders()
完全相同的数据,因为Hierarchy
继承了实现了ILoggerRepository
(由Logger.Repository
返回的类型)的LoggerRepositorySkeleton
.
you are in fact asking the exact same data as hierarchy.GetAppenders()
because Hierarchy
inherits LoggerRepositorySkeleton
, which implements ILoggerRepository
, the type returned by Logger.Repository
.
但是,您可以使用Hierarchy
命名空间中的Logger
类来获取第一级"附加程序的列表:
You can however get the list of "first level" appenders by using the Logger
class that lives in the Hierarchy
namespace:
var h = LogManager.GetRepository() as Hierarchy;
var l = h.GetLogger("MyLoggerName", h.LoggerFactory);
// do something with the l.Appenders property
您将不得不处理诸如bufefring或从那里过滤追加程序之类的特殊情况
You will have to handle special cases like bufefring or filtering appenders from there
这篇关于log4net-仅针对一个记录器获取附加程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!