An application needs a counter, which is a value stored in a text file, sometimes at very short intervals it happens.
This test code rewrites a text file (like every 100 milliseconds):
int counter = 0; While (true) {WriteToFile (counter); Counter ++; Thread.Sleep (100); } Private Zero WriteToFile (int counter) {byte [buffer = encoding]. ASCII.GetBytes (counter.ToString ()); (Filestream vs. file = new filestream ("counter.txt", fliemmod.creative)) {createFile.Write (buffer, 0, buffer length); CreateFile.Close (); }}
This works basically fine except for one of our "trusted tests" - the computer is running, while the application is running.
Bad Wonders - There was no text in the file (should be a number), but only one or two space characters.
This missing last value that tries to write can be understood, but it is really bad to not make the file compatible.
Try to use it:
createFile.Flush (); CreateFile.Close (); CreateFile.Dispose (); GC.Collect (); GC.WaitForPendingFinalizers ();
does not help.
Keep in mind the various options: -
1) One of the current files Keep a temporary copy, eg rename the old file, write a new file, delete the old file (or write a new file with a temporary name, delete the old file, change the name of the new file), but maybe you would like to file
2) With a periodic clean, example, write multiple files 0001.txt, 0002.txt, 0003.txt, ... e.t.c. But delete the last file every 10 seconds.
3) Write the same file multiple times, perhaps by creating a new file from time to time, every 10 minutes is said to be done again, by repeating the periodic cleansing.
4) Use third-party logging platforms like log-ins which probably protect against things like power upgrades, and can enter different targets, e.g. File, database, IRC
Comments
Post a Comment