Performance issues with sing LINQ to XML for logging

Posted on 2011-10-06
Last Modified: 2013-12-16
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!!
Question by:techsuppoprt
    LVL 96

    Expert Comment

    by:Bob Learned
    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?
    LVL 1

    Author Comment

    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.
    LVL 96

    Expert Comment

    by:Bob Learned
    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.
    LVL 1

    Author Comment


    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 ?
    LVL 96

    Accepted Solution

    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

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now