I'm using XML Writer to create a log of some important events in my application.
Currently it all works fine assuming that the application is correctly closed, but if it isnt, the file isnt closed and the log is basically lost.
Let's assume the writing of a simple element, something like this:
writer.WriteStartElement(eventName);
writer.WriteAttributeString("t", DateTime.Now.ToString());
writer.WriteString(eventBody);
writer.WriteEndElement();
Is there any way to close the file at this point and append the remaining elements to it later and only then closing the root element and xml writer?
You can call the method writer.Flush() after your block of instructions. This should write the log and you won't lose any element.