tags:

views:

72

answers:

3

Hello,

I'm trying to write a little software for logging data, if any type of file created in directory A.

I use FileSystemWatcher class to get information about file creation in folder A. There are many subfolder in folder A. And many users can create file in this directory in one time.

I use XML data to save log data.

XmlTextReader reader = new XmlTextReader(FILE_NAME);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
reader.Close();
XmlNode currNode;

XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
docFrag.InnerXml = "<item>" +
"<path>" + fileName + "</path>" +
"<created>0</created>" +
"<date>" + DateTime.ParseExact(DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"), "dd.MM.yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None).ToString() + "</date>" +
"</item>";
// insert the availability node into the document
currNode = doc.DocumentElement;
currNode.InsertAfter(docFrag, currNode.LastChild);
//save the output to a file
doc.Save(FILE_NAME);

But sometimes while occurs watcher.Created += new FileSystemEventHandler(OnChanged);, data about file creation is not inserted to XML file.

So, is it possible if file opened for data writing, and it's locked for new dataWrite file document not saved? and how to fix this.

+1  A: 

Seems that sometimes you open file several times and between this operations file is in different states, so when you write it you just overwrite some data written before. I propose you to collect changes in Queue and write them to the file sequentially.

Restuta
@Restura, thanks. I've just created 1000 txt files with C# method. Problem occurs. +1 for advice about Queue.
loviji
So are you going to try my solution?
Restuta
We're going to write Application Service that will create Application Log file.And then read data from Application Service Log and Write to XML file every 30 second. so XML file will not be in different states.
loviji
A: 

If you need to log data very often, you should either write to database instead of writing to a file or use buffering.

GôTô
which database you prefer to write simple data? If software to small and has not big functionality?
loviji
Don't you have an existing DB you could use?
GôTô
I've many DB, but I don't want to use DB for this little software, and depend software from another db software.
loviji
+2  A: 

You are in front of a beauty computer problem, please read a bit about the Dining Philosophers problem in http://en.wikipedia.org/wiki/Dining_philosophers_problem .

You can "lock" a file just setting its attribute to ReadOnly

I mean, when you are going to write, you check if "ReadOnly" is set. In that case

System.IO.File.SetAttributes("pathtofile\filename.ext", FileAttributes.ReadOnly);

After writing, please remove the attribute.

A more complex solution could be the use of semaphores, thus controlling yourself the files that are being accessed. You can find a hint here in StackOverflow:

http://stackoverflow.com/questions/2277603/problem-with-using-semaphore-to-protect-queue

Also, you can use this link as a hint to really lock files:

http://stackoverflow.com/questions/1432050/how-to-lock-file

Hope that helps,

Ramon Araujo
@Ramon, thanks for your advice, and +1 for wiki page.
loviji