log4net 附加程序可以在自己的 XML 文件中定义吗?

发布于 2024-08-31 21:59:14 字数 1676 浏览 4 评论 0原文

我想在我的库中定义一些 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云雾 2024-09-07 21:59:14

我会执行以下操作:

  • 读取所有 xml 片段
  • 自己将它们(使用 linq 或其他方式)组合到有效的 log4net 配置“文件”(当然在内存中)
  • 将其提供给 log4net

I would do the following:

  • read all xml fragments
  • combine them yourself (with linq or whatever) to a valid log4net configuration "file" (in memory of course)
  • feed that to log4net
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文