log4net 附加程序可以在自己的 XML 文件中定义吗?
我想在我的库中定义一些 (ADO.NET) 附加程序,但允许我的库的用户配置这些附加程序的使用。像这样的东西似乎就是我想要的:
XmlConfigurator.Configure(appenderStream1);
XmlConfigurator.Configure(appenderStream2);
XmlConfigurator.Configure();
但这似乎不起作用,尽管调试输出包含如下消息:
配置更新模式[合并]。
这样做的正确方法是什么?或者,除了要求用户复制大块 XML 配置之外,还有其他选择吗?
解决方案:
感谢Stephan的建议,我能够实现以下日志初始化方法。它首先将 .config 部分读入 XmlDocument,然后将外部附加程序(在我的例子中是嵌入式资源)添加到文档中,最后将全部内容提供给 log4net。
static readonly string[] AppenderConfigs =
{
"Logging.EntryPointAppender.xml",
"Logging.TracingAppender.xml",
"Logging.MessagesAppender.xml",
};
public static void Initialize()
{
// this first bit reads the log4net config in the application's config file
var section = (XmlElement)ConfigurationManager.GetSection("log4net");
if (section == null)
return;
XmlDocument xml = new XmlDocument();
using (var xml_reader = new XmlNodeReader(section))
xml.Load(xml_reader);
// then we augment the above XML with the content of our embedded resources
foreach (var appender_config in AppenderConfigs)
using (var stream = EmbeddedResource.Open("My.Assembly", appender_config))
AddToConfig(xml, stream);
XmlConfigurator.Configure(xml.DocumentElement);
}
static void AddToConfig(XmlDocument config, Stream xmlStream)
{
var fragment = config.CreateDocumentFragment();
using (var reader = new StreamReader(xmlStream))
fragment.InnerXml = reader.ReadToEnd();
config.LastChild.AppendChild(fragment);
}
I want to define a handful of (ADO.NET) appenders in my library, but allow users of my library to configure the use of those appenders. Something like this seems to be what I want:
XmlConfigurator.Configure(appenderStream1);
XmlConfigurator.Configure(appenderStream2);
XmlConfigurator.Configure();
But that doesn't seem to work, in spite of the debug output containing messages like this:
Configuration update mode [Merge].
What is the right way to do this? Or, is there an alternative to asking users to duplicate large chunks of XML configuration?
Solution:
Thanks to Stephan's suggestion, I was able to implement the following log initialization method. It first reads the .config section into an XmlDocument
and then adds external appenders (which are in my case embedded resources) to the document, finally feeding the whole lot to log4net.
static readonly string[] AppenderConfigs =
{
"Logging.EntryPointAppender.xml",
"Logging.TracingAppender.xml",
"Logging.MessagesAppender.xml",
};
public static void Initialize()
{
// this first bit reads the log4net config in the application's config file
var section = (XmlElement)ConfigurationManager.GetSection("log4net");
if (section == null)
return;
XmlDocument xml = new XmlDocument();
using (var xml_reader = new XmlNodeReader(section))
xml.Load(xml_reader);
// then we augment the above XML with the content of our embedded resources
foreach (var appender_config in AppenderConfigs)
using (var stream = EmbeddedResource.Open("My.Assembly", appender_config))
AddToConfig(xml, stream);
XmlConfigurator.Configure(xml.DocumentElement);
}
static void AddToConfig(XmlDocument config, Stream xmlStream)
{
var fragment = config.CreateDocumentFragment();
using (var reader = new StreamReader(xmlStream))
fragment.InnerXml = reader.ReadToEnd();
config.LastChild.AppendChild(fragment);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会执行以下操作:
I would do the following: