tags:

views:

363

answers:

2

Is it a bad idea to ensure that a log directory exists before every log message in an application that could be logging a few times a second (though not continually)?

I could implement a File System Watcher style thread to fire up and recreate a lost log directory, but my gut feeling is that would be a bit heavier an operation

+4  A: 

I would check to see if it exists on the first time, such as the construction of your logger, if not create it then.

Windows cannot easily delete a directory that is in use.

Daniel A. White
The directory would not necessarily be in use as my StreamWriter is handled in a using, therefore is disposed after each log
johnc
additionally if you maintain a file lock in a directory it cannot be deleted until the process holding the lock is terminated.
CptSkippy
+1 I agree, check only once and then lock the log file so the dir cant be deleted.
Darko Z
When you create the FileStream Object, specify OpenOrCreate as the FileMode parameter and you're set.
CptSkippy
I'd recommend taking a look at Log4Net to see how they do it. I am in fact watching our 3 GB Log4Net log for this evening tail by at this moment.
cfeduke
+1  A: 

Probably not that expensive. If you did it by just handling the exception on the failed write, you would only get the exception the first time through (or after the directory is removed) and then everything else would be straight through. That might be better than continually checking.

JP Alioto
Not a bad thought at all
johnc
That kind of makes me think that Java should have syntax that looks like: try{stuff} catch(Exception e){catch code}andRetryOnce. Otherwise this type of code ends up with some strange exception code and a loop that can't easily be isolated into a library.
Bill K
thinking largely the same thing
johnc