Problem with LINQ XML logging

Hello Experts

I'm using LINQ to XML I have a bit of a problem with writing to a XML file. It seems that instead of inserting XML nods the framework is writing to the XML file as a streamer, line by line.

I'm using a XML file to record my program events aka logging. If the program is closed before all  processes are finished the XML logging file ends up being unfinished.

Example:
Initial log file:

<root>
<processInfo>
<\processInfo>
 <events>
  <event>
 //Logging event info
  <\event>
 <\events>
<root>

I would expect, as the process runs, for the program to keep the tags and just keep on adding <event> nods with log entries ( and it works fine if I let it run ) but if the process is interrupted I end up with something like this:

<root>
<processInfo>
<\processInfo>
 <events>
  <event>
  This is my logging event info. It was cut off in the mid
 
How can I avoid this problem and make sure that even if my program process is interrupted the XML Logging file closing tags are still there ?

Thank you.
<root>
<processInfo>
<\processInfo>
 <events>
  <event>
 //Logging event info
  <\event>
 <\events>
<root>

Open in new window

<root>
<processInfo>
<\processInfo>
 <events>
  <event>
  This is my logging event info. It was cut off in the mid

Open in new window

LVL 1
techsuppoprtAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wdosanjosCommented:
Your logging class should implement IDisposable and have the necessary code in the Dispose method to properly finalize the file.

IDisposable Interface (System)
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx
dimajCommented:
I've written a small blog article on how to manipulate XML files using LINQ. You can find it here: http://blog.dimaj.net/2011/02/howto-update-xml-file-using-linq/
techsuppoprtAuthor Commented:
The blog doesn't help much. It just has basic info how to use XDocument.Save().

Wdosanjos, I thought about collecting process log entries in a collection and then recording them to the XML file only at the using when Dispose() gets called. But what if the process freezes or user desires to end task it.. there will be no log file created correct ?
wdosanjosCommented:
Correct.  I meant the code in the Dispose method should complete the xml file (closing  <\events> and </root> tags) and closing the file.  As you pointed out buffering all the entries in memory and only writing at the very end won't work consistently.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.