Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4net xml output

Tags:

xml

log4net

I want full control over log4net xml output.

How is it possible to customize the output template?

like image 535
JL. Avatar asked Jul 18 '09 09:07

JL.


People also ask

What is log4net 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. You can also tell the compiler not to emit XML documentation for a project in that project's properties.

How do I create a log4net config file?

config File. Add a new file to your project in Visual Studio called log4net. config and be sure to set a property for the file. Set Copy to Output Directory to Copy Always.

Where are log4net logs stored?

The log4net. config file is located in the \Enterprise7\bin\ directory and is the configuration file for the log4net logging application.

Where do I put log4net in web config?

Now, add the section "<log4net></log4net>" after the <configSections/> element in your app. config file. Next, inside the "<log4net></log4net>" section, place the configuration details as shown in the code snippet given below. That's all you need to do to configure log4net.


3 Answers

As suggested by MrPeregrination you need to write a class deriving from XmlLayoutBase, override the FormatXml method and instruct your appender to use it as layout:

class Program
{
    static void Main(string[] args)
    {
        XmlConfigurator.Configure();
        ILog log = log4net.LogManager.GetLogger(typeof(Program));
        log.Debug("Hello world");
    }
}

public class MyXmlLayout : XmlLayoutBase
{
    protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
    {
        writer.WriteStartElement("LogEntry");
        writer.WriteStartElement("Message");
        writer.WriteString(loggingEvent.RenderedMessage);
        writer.WriteEndElement();
        writer.WriteEndElement();
    }
}

And in app.config put this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

  <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="log.txt" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="MyNamespace.MyXmlLayout" />
    </appender>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
  </log4net>
</configuration>

This will produce entries like this in your log file:

<LogEntry><Message>Hello world</Message></LogEntry>
like image 155
Darin Dimitrov Avatar answered Oct 31 '22 11:10

Darin Dimitrov


Adding to Darin's answer, in log4net 2.0.8, I had to change the layout a little:

<layout type="MyNamespace.MyClass, MyNamespace"/>

I found this here https://www.codewrecks.com/post/old/2008/04/writing-a-custom-formatter-for-log4net/

like image 25
devhl Avatar answered Oct 31 '22 11:10

devhl


Check out the XmlLayoutBase class. I think thats probably what you need. There's a FormatXML function you will need to override to supply the XmlWriter with the correctly formatted data.

like image 4
Russell Troywest Avatar answered Oct 31 '22 11:10

Russell Troywest