Is it possible for Log4Net to add to a well-structured XML document?

The problem I am facing is that it simply writes XML tags to a file, but it is not a properly formatted XML file.

The file is simply populated as follows:

<LogEntry><Date>12/04/2012 11:16:26</Date><Message>An error message.</Message><StackTrace>..StackTrace omitted..</StackTrace></LogEntry>
<LogEntry><Date>12/04/2012 11:21:27</Date><Message>Another error message.</Message><StackTrace>..StackTrace omitted..</StackTrace></LogEntry>
<LogEntry><Date>12/04/2012 11:21:37</Date><Message>More messages.</Message><StackTrace>..StackTrace omitted..</StackTrace></LogEntry>

Is there a way to add to a well-structured XML document?

Ideally, it looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Logs>
    <LogEntry><Date>12/04/2012 11:16:26</Date><Message>An error message.</Message><StackTrace>..StackTrace omitted..</StackTrace></LogEntry>
    <LogEntry><Date>12/04/2012 11:21:27</Date><Message>Another error message.</Message><StackTrace>..StackTrace omitted..</StackTrace></LogEntry>
    <LogEntry><Date>12/04/2012 11:21:37</Date><Message>More messages.</Message><StackTrace>..StackTrace omitted..</StackTrace></LogEntry>
</Logs>

The code I use is:

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

        writer.WriteStartElement("Date");
        writer.WriteString(loggingEvent.TimeStamp.ToString(CultureInfo.CurrentCulture));
        writer.WriteEndElement();

        writer.WriteStartElement("Message");
        writer.WriteString(loggingEvent.RenderedMessage);
        writer.WriteEndElement();

        writer.WriteStartElement("StackTrace");
        writer.WriteString(loggingEvent.ExceptionObject.StackTrace);
        writer.WriteEndElement();

        if (loggingEvent.ExceptionObject.InnerException != null)
        {
            writer.WriteStartElement("InnerException");
            writer.WriteString(loggingEvent.ExceptionObject.InnerException.ToString());
            writer.WriteEndElement();
        }

        writer.WriteEndElement();
    }
}

and configuration for this:

<log4net>
  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
    <param name="File" value="Logs/log.xml" />
    <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>
+3
source share
1 answer

The class XmlLayoutBasedoes not support what you are looking for. The reason is quite simple: if he had to make sure that xml was always well formatted, then to write to the file, it would be necessary to overwrite the closing root of the node. Simply adding an xml fragment to a file is simpler and faster.

+5
source

All Articles