Create a XML-File in an exception safe and memory friendly way?

224 views Asked by At

I want to write a Logger/ Bug-Tracker with XML output for my current project. I'm sorry if it should be a duplicate, but the proposals were not useful and I did not found a good google solution too.

1: My first question is about exception safety.

If i use a XmlDocument the loggs are stored in memory till i call save. That means i could lose everything in case of an exception.

If I use a XmlWriter it's not stored in memory (afaik) but I have to close the writer and all elements / nodes which may be a lack in case of an exception too. Can I close and re-open the writer (with the pointer at the end of the document) ?

What's the best solution for an exception-safe XML creation ? (I only need a hint)

2: My second question is about memory usage.

Because it's a tracing tool the output can be very huge. Therefore I can't use XmlDocument. In my opinion the XmlWriter would be the best solution for this. Am i right with this ?

3: My last [minor] question is about time consumption.

Is it a good or bad idea to use a XML file for tracing? How much does it slow down my program ?

I hope you can help me.

EDIT: why do I want to use XML ? later on, my app will run in an "unknown" environment therefore it is neccessary that I can send the log over internet to a server and I have to validate the file (XMLSchema). After this is done I want to convert it to a better readable (and nice formatted) HTML file.

My HTML file

As you can see, this is a much better visualization than XML (this still need some fine tuning but it's functional)

EDIT 2: current state I have made some memory usage measurements. The Logger (currently based on XmlDocument :( ) need ~600mb for 5.000.000 entries. Not the best result but not the worst too.

Best regards Alex

3

There are 3 answers

0
Alex On BEST ANSWER

Because this question did not contains a good answer, i have to answer it myself. I've dicided to use a ringbuffer with 10k entries for my log (this seems to be a good number in live use) and i've tried to build a thread and exception safe logbuf (i hope it works).

best regards and thx for all answers

Alex

1
Paul Eden On

For a trace file, do you need the structure of XML? What are your plans for the file once it has been produced; if it is purely for human processing then a text file would be sufficient? That way you can flush after every write. If you want a more queryable file format, could you incorporate a light DB engine? Before changes to MDAC and 64-bit issues with MSAccess file accessing, I used to write to an mdb file. More recently I looked as SQLite and VistaDB.

Is there a specific reason behind you opting for XML?

3
Christian Sauer On

Why do want to roll your own logging solution? There are very mature libraries which can do it "out of the box" afaik.