Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net - Can I have a custom section name in Config

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" />

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

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

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

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); 

Can anyone help please.

like image 992
Web Application Developer Avatar asked Sep 19 '13 23:09

Web Application Developer


People also ask

Where do I put log4net in web config?

Add log4net in config file config and enter the following details. Add a class Log. cs in the Utilities folder. Now, in the constructor of this class, instantiate logs for monitoring and debugger loggers.

Does log4net need XML?

It's probably an XML documentation file, which contains the XML comments on all of the members in the log4net project. It's not at all necessary for installation, so you can delete it if you want.

What are log4net Appenders?

For log4net to know where to store your log messages, you add one or more appenders to your configuration. An appender is a C# class that can transform a log message, including its properties, and persist it somewhere. Examples of appenders are the console, a file, a database, an API call, elmah.io, etc.


1 Answers

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.

What you can try to do is to:

  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); 
like image 143
Marek Musielak Avatar answered Sep 30 '22 05:09

Marek Musielak