I'm using File.Exists
in C# to determine if a dynamically named log file is present, then using File.Create
to create the file if it is not. The file naming scheme is Logfile_20241009.txt
with the numbers being dynamically generated using the date. My only concern is that two requests might occur at exactly the same time for the first entries of the day, and the record of one might be overwritten by the other. Would this throw some kind of exception I can check for? Am I overthinking this?
I'm using File.Exists in C# to determine if a dynamically named log file is present, then using File.Create to create the file if it is not.
...
Am I overthinking this?
It's almost always wrong to check File.Exists()
before accessing a file in the first place. The file system is volatile, where the presence of a file can change between the line where you check it and the line where you use it. Additionally, existence is only one reason among several you may have an issue. So when it comes down to it, you still need an exception handler.
Once you have the exception handler, the File.Exists()
check is not only redundant, but it's extra drive I/O, which is nearly the slowest thing you can do in a single computer... even slower and more wasteful than the stack unwinding needed to handle an exception, which is the main reason we avoid exceptions for control flow.
Additionally, the File.Append_()
family of methods will already create a new file if needed.
In other words, you can reduce complexity and improve performance in your code with a pattern like this:
void LogMessage(string Message)
{
var fileName = Path.Combine("base path here", $"Logfile_{DateTime.Now:yyyyMMdd}.txt");
try
{
File.AppendAllText(fileName, Message);
}
catch(Exception ex)
{
// decide how to handle failures here
// ... or not. It may be best to just let them bubble up to the main application
}
}
Note that File.AppendAllText()
will already create the file if needed:
Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file
(emphasis mine)
But I can hear it now: "If we shouldn't use File.Exists()
, why does the method exist at all? What's it good for?"
It's useful when the mere presence of a file, without ever needing to access it, is enough to trigger a process, especially related to complex system orchestration tasks. For example, one system may write out a file, and a separate system may watch for it's creation, where the new file creation alone, separate from the contents, is enough to tell it to go to the next phase.
This is common in (among other things) manufacturing, where an isolated machine controller program may write a file when a job completes, and your program wants to spot this and use it to trigger an event elsewhere, such as an alert to an operator or another machine to continue the next step. Or you may have a monitoring solution watching log files to sends a daily notice or update a dashboard if anything is missing, where failing a File.Exists()
check causes an alert, but otherwise the contents of the file don't matter.