The problem I’m having is that it just writes XML tags to a file but it’s not a correctly formatted XML file.
The file is just being 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 any way for me to append to a correctly structured XML document?
Ideally it would appear as follows:
<?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’m using 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 the configuration for this is:
<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>
The
XmlLayoutBaseclass does not support what you are looking for. The reason is quite simple: If it had to make sure that the xml is always well formatted, then it would need to overwrite the closing root node whenever an entry is added to the file. Simply appending an xml fragment to the file is easier and faster.