Avatar of techsuppoprt
techsuppoprt

asked on 

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.

So..
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.Add()
Xdocument.Save() which, as mentioned deletes and overwrites the XML file, affecting my program performance.

Thank you!!
.NET ProgrammingC#

Avatar of undefined
Last Comment
Bob Learned
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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?
Avatar of techsuppoprt
techsuppoprt

ASKER

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.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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.
Avatar of techsuppoprt
techsuppoprt

ASKER

Yup.

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())
            {
                xmlLogFile.Load(xmlReader);
            }

So Now I just need to figuire out how to append my XElements to this new XElement file/Stream them in.
Suggestions ?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo