在运行时附加或分离附加程序 [英] Attach or detach appender at runtime
问题描述
我正在为log4net使用XML配置.我有以下的appender-refs:
I'm using XML configuration for log4net. I have the following appender-refs:
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="OldRollingLogAppender" />
</root>
(我在XML中也定义了这些追加器,它们的确切实现无关紧要.)
(I also have these these appenders defined in the XML—their exact implementation shouldn't matter.)
我想在运行时删除OldRollingLogAppender
,并创建并添加一个名为NewRollingLogAppender
的新文件.我该怎么做呢?
I'd like to, at runtime, remove OldRollingLogAppender
, and create and add a new one called NewRollingLogAppender
. How do I do this?
一个最小的工作示例会很好.
A minimal working example would be nice.
推荐答案
您可以通过获取root记录器,然后添加和删除附加程序来做到这一点:
You can do this by getting the root logger and then add and remove the appenders:
删除:
var root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;
root.RemoveAppender(xxx);
添加:
var root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;
root.AddAppender(xxx);
您可以使用root迭代附加程序列表. https://logging.apache.org/log4net/release/sdk/html/AllMembers_T_log4net_Repository_Hierarchy_RootLogger.htm
You can iterate the list of appenders with root.Appenders https://logging.apache.org/log4net/release/sdk/html/AllMembers_T_log4net_Repository_Hierarchy_RootLogger.htm
这篇关于在运行时附加或分离附加程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!