本文介绍了Log4Net-我可以在Config中有一个自定义的部分名称吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在config部分使用除log4net以外的其他部分名称.我知道这就是我们通常使用的

I have a need to use section name other than log4net in the config section. I know this is what we generally use

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

但是我需要有一个这样的部分

But I need to have a section like this

<section name="log2net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

我正在一个sitecore网站上工作,它有自己的Sitecore.Logging dll,它也源自log4net.因此,Sitecore日志记录dll引用了web.config中的log4net部分

I am working on a sitecore website and it has its own Sitecore.Logging dll which is also derived from log4net. So the Sitecore Logging dll is referring to section log4net in web.config

我们有自定义的log4net附加程序,该附加程序仅适用于log4net,不适用于sitecore.logging dll.所以我以为我的项目中可以有两个记录器,sitecore.logger和log4net记录器. Sitecore.logger使用log4net部分,因此我希望log4net使用其他部分,例如log2net

We have our custom log4net appender that works only with log4net and not with sitecore.logging dll. So I thought I can have two loggers in my project, sitecore.logger and log4net logger. Sitecore.logger uses log4net section so I wanted log4net to use a different section anme like log2net

我试图通过在配置中使用log2net部分来使用以下代码.

I tried to use the below code by having log2net section in config.

但是我收到错误log4net:ERROR XmlHierarchyConfigurator:Xml元素是-不是log4net元素.

But I get the error log4net:ERROR XmlHierarchyConfigurator: Xml element is - not a log4net element.

 XmlElement element = (XmlElement)ConfigurationManager.GetSection("log2net");
        log4net.Config.XmlConfigurator.Configure(element); 

任何人都可以帮忙.

推荐答案

我无法重现您遇到的异常,但请查看其详细信息和 XmlHierarchyConfigurator的代码,当根xml元素名称不是log4net时抛出此异常正是您要尝试做的.

I wasn't able to reproduce the exception you're experiencing but looking at its details and the code of XmlHierarchyConfigurator class, the exception is thrown when the root xml element name is not log4net and this is exactly what you're trying to do.

您可以尝试做的是:

  1. 阅读您的自定义log2net XmlElement
  2. 创建一个新的log4net XmlElement
  3. log2net的所有子代复制到新的log4net元素
  4. 执行XmlConfigurator.Configure()方法,并传递新的log4net元素.
  1. Read your custom log2net XmlElement
  2. Create a new log4net XmlElement
  3. Copy all the children of your log2net to the new log4net element
  4. Execute XmlConfigurator.Configure() method passing your new log4net element.
XmlElement element = (XmlElement)ConfigurationManager.GetSection("log2net");

XmlElement newLog4net = element.OwnerDocument.CreateElement("log4net");

for (int i = 0; i < element.ChildNodes.Count; i++)
{
    XmlNode child = element.ChildNodes[i];
    newLog4net.AppendChild(child.CloneNode(true));
}

log4net.Config.XmlConfigurator.Configure(newLog4net); 

这篇关于Log4Net-我可以在Config中有一个自定义的部分名称吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 11:41