tags:

views:

423

answers:

4

I need to write an error log to a XML file using Visual Basic 6.0. Is there an easy way to do this?

The error log will contain the error number, source, desc, and a timestamp.

Thanks, Jeff

+4  A: 

Probably it's best to use Microsoft's MSXML libraries to do that.

There is a sample in the knowledge base: http://support.microsoft.com/kb/286817

0xA3
A: 
gimpf
Avoiding the overhead of creating the XML parser just to create a simple XML document is very good advice!
Jim Blizard
@gimpf: Tweak function to handle proper encoding of description
AnthonyWJones
"Wow, Thanks for the quick response."Sincerest apologies to Binary Worrier and the entire community.
well-formed xml also has a root node, so the closing tag for the root node must be overwritten and then appended each time you add an entry as well.
Joel Coehoorn
I would not recommend this way. It may seem simpler at first but it's easy to break the XML document, e.g when "description" contains an invalid character such as '<'
0xA3
(Continued:) And as Joel says the well-formedness is harder to maintain. That's why XML log files are generally a bad idea (Appending is expensive)
0xA3
The idea to use XML for logging is debatable, yes.I'd never write a closing tag for the XML file itself -- in case of a program crash this cannot be guaranteed nevertheless, so the reader has to take care of that.
gimpf
Valid XML also must be Unicode, which is going to be difficult. I also agree that you shouldn't use XML for logging.
MarkJ
Unfortunately, XML is required. However, the XML log file is temporary storage to ensure the information gets in the database. Every error will have its own XML file and will be deleted once the error is logged in the DB.
A: 

Wow, Thanks for the quick response.

Dude, have a read of the faq. You shouldn't "communicate" with others by adding an "answer", you can leave comments on answers (for your own questions) and you can edit your own question to add more detail if necessarry
Binary Worrier
+1  A: 

The Microsoft MSXML libraries are your first port of call, but there are things you need to consider before you decide to use it.

Appending error messages to an Xml using the MSML library will parse and load the Xml file every time you open it. As the Xml file grows you will find your app slows to a crawl.

It would be good if you could "just" append your error to the end of the file, however because of closing tags, this isn't straightforword, but it can be done.

If you have control over how the Xml file is created, you can work around this performance bottle next by splitting the Xml file into two, one with the header and one with the body of the xml. Then you can append to the end of the body file (by using an Xml writer class, or by simply appending text to the file).

See Efficient Techniques for Modifying Large XML Files for more info

However all this is unnecessarry if you're writing one error to the xml file, and the next error goes into another Xml file. In that case, go with MSXML

Hope this helps.

Binary Worrier