log4net - Append errors to an xml file



I am trying to append error to an xml file, where I am not able to append the way I wanted.


I am using custom layout overriding XmlLayout format method as shown below.



public class MyXmlLayout : log4net.Layout.XmlLayout
{
public static bool isFirstTime = true;

protected override void FormatXml(System.Xml.XmlWriter writer, LoggingEvent loggingEvent)
{
if (isFirstTime)
{
writer.WriteStartDocument();
writer.WriteStartElement("Exceptions");
}

writer.WriteStartElement("Exception");

writer.WriteStartElement("Error");
writer.WriteAttributeString("Date", loggingEvent.TimeStamp.ToUniversalTime().ToString());
writer.WriteAttributeString("User", loggingEvent.UserName);
writer.WriteString(loggingEvent.RenderedMessage);

writer.WriteEndElement();
writer.WriteEndElement();

if (isFirstTime)
{
writer.WriteEndElement();
writer.WriteEndDocument();

isFirstTime = false;
}
}
}


Yes, Its appending through the above code, but the problem is I am unable to read xml file as its not in proper format.


In the below screenshot, Left is the generated one and the right is the correct fixed one.


enter image description here


Please help us with the solution.


No comments:

Post a Comment