Deleting Log File Programmatically


tamm

Recommended Posts

Hi.

I have a document with a button to remove all history from all channels. Since one of my channels contains string data, I have a logging set for that channel in order to be able to recover (persist) its contents over a restart of DF. So, I have to clear not only the channel but also its log file.

Unfortunately, this file is locked by DF and cannot be removed, I get the error "F0011 SHARE.EXE was not loaded, or a shared region was locked."

I have found the following post:

http://www.azeotech.com/board/index.php?/topic/3698-logging-file-available-read-only/

but this does not work reliably for me. When I change the logging set's file name, normally nothing happens, and even after an endlogging(...) and beginlogging(...), the old file is still locked.

Any idea how I may delete that log file from code?

Link to comment
Share on other sites

The logging set won't change the filename until it tries to log another line.  So, if you have stopped adding new values to the channel, and then rename the logging set, it won't actually close the file because the logging set doesn't get new values.  So, just rename the file, then stick another value in the string channel (use addValue() if you have to), wait 1 second then delete the file, rename the logging set back and clear the history.

Link to comment
Share on other sites

The logging set won't change the filename until it tries to log another line.  So, if you have stopped adding new values to the channel, and then rename the logging set, it won't actually close the file because the logging set doesn't get new values.  So, just rename the file, then stick another value in the string channel (use addValue() if you have to), wait 1 second then delete the file, rename the logging set back and clear the history.

Thank you for taking your time for this issue. Unfortunately, this is still not working reliably for me, and behaves quite unpredictably. The following code seems to work quite ok right now:

// remove all history
Private tmp=Logging.notes.strFileName
Logging.notes.strFileName="C:\dummy.tmp"
delay(1)
Notes.AddValue("Dummy entry")
delay(1)
Notes.AddValue("Dummy entry")
delay(1)
try
   File.Delete(tmp)
catch()
   System.MessageBox("Kan inte ta bort filen "+tmp+". Avsluta programmet, ta bort filen manuellt och starta om programmet.")
endcatch
Logging.notes.strFileName=tmp
Notes.ClearHistory()

Interestingly, after this sequence, the dummy file does not contain any Dummy entry. However, a new file is created instantaneously with the original file name (C:\notes.csv in my case) containing one Dummy entry line. I added the try...catch just in case it does not work, but at least now the MessageBox does not get displayed.

So to be clear, this works acceptable for me, but maybe you are interested to investigate the issue for your own sake.

Link to comment
Share on other sites

Logging sets are really designed for more basic implementations: start logging, log forever, maybe stopping occasionally.  If you really want tight control over things, you should create your own sequence and use the file. functions to write the logging file yourself.  Then you'll have total control over opening and closing of files and deleting them.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.