Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:41:47+00:00 2026-06-01T23:41:47+00:00

The problem I’m having is that it just writes XML tags to a file

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:41:49+00:00Added an answer on June 1, 2026 at 11:41 pm

    The XmlLayoutBase class 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: find ids that are in one file but not in another. Each file
Problem: to show that a Not-SO-page has been discussed in SO when you at
Problem description: Read an xml file, traverse to a particular node (element), if it
Problem: I can set the exposureMode property of AVCaptureDevice, but it does not stay
Problem: Trying to create a Mix that is applied to the AVPlayerItem, but it
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem: I am having trouble implementing a recursive image lazy load in all relevant
Problem When working with MasterPages, a common irritation I run into is that script
Problem: I'm working on a website where there is a dial that displays a
Problem! I Have the following input (rules) from a flat file (talking about numeric

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.