Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

Performance issues with sing LINQ to XML for logging

Hi Experts.

I need to figure out a way to append to a XML file instead of overwriting it while recording my "logging events".

As the logging process is running overwriting the file with XDocument.Save(FilePath) function the process gets slower and slower and slowweeeerrrr as the XML file keeps on growing. I realize it's happening because XDocument.Save() function completely wipes and overwrites the XML file every time its executed.

I have an <Event> XElement .
Somewhere within the XDocument I have a <Events> tag that I need to put it under.

How can I "Stream" my Xelement to the right XDocument tag without having to do
Xdocument.Save() which, as mentioned deletes and overwrites the XML file, affecting my program performance.

Thank you!!
  • 3
  • 2
1 Solution
Bob LearnedCommented:
Why are you using XML document?  That doesn't sound like a good choice for logging, since XML adds a lot of overhead.  Would something like log4net, with a FileAppender, be a better choice?
techsuppoprtAuthor Commented:
Agree, not the best way but that's what it is.

The question is how can I stream XElements to an XML file instead of overwriting it every time with .Save() function?

Thank you.
Bob LearnedCommented:
I don't think that there is a way to stream XML, without loading all the XML into the XmlDocument.  That would be a good research question.
techsuppoprtAuthor Commented:

That's exactly what I thought and already working on it:

Converting XDocument to XMLDocument:
//Convert Xdocument to XMLDocument
           var xmlLogFile = new XmlDocument();

            using (var xmlReader = LogFile.CreateReader())

So Now I just need to figuire out how to append my XElements to this new XElement file/Stream them in.
Suggestions ?
Bob LearnedCommented:
You would need to grab a reference to the XElement that you need, and then call the Add method with the new elements that you want to add (this can be a completely new branch hierarchy), and Save.

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now