Reputation: 919
I have a program that employs a simple logging process. The idea is that every day a new file gets created that corresponds with the date unless the file already exists, in which case it's just appended to. The problem is, File.Create is throwing an error every time it runs. I found a solution to the problem that says File.Create is opening a FileStream and you just need to call it with a .close(); but that solution didn't work for me, I'm still getting the IO exception saying that the file is in use by another process, which is impossible since it doesn't exist until File.Create creates it, and nothing else uses these files.
Here's the code:
public static void logResults(System.Reflection.MethodBase method, Results result, string message)
{
string date = DateTime.Now.ToString();
int index = date.IndexOf(" ");
string subString = date.Substring(0, index);
string nwDate = Regex.Replace(subString, "/", "");
logFileName = "WebsiteRegressionProduction_TestCycle." + nwDate;
string currentLogFile = logFileLocation + @"\" + logFileName;
StringBuilder sb = new StringBuilder();
if (!File.Exists(currentLogFile))
{
File.Create(currentLogFile).Close();
sb.Append("DATE-TIME\tACTION\tTEST CLASS\tTEST NAME\tTEST STATUS\tERROR MESSAGES");
sb.Append("\n");
}
else
{
string previousLogs = File.ReadAllText(currentLogFile);
sb.Append(previousLogs);
}
sb.Append(DateTime.Now.ToString());
sb.Append(" : ");
sb.Append("Text Executed: ");
sb.Append(method.ReflectedType.Name + " : ");
sb.Append(method + " : ");
sb.Append(result.ToString());
sb.Append(" : ");
sb.Append(message);
sb.Append("\n");
sb.Append("\n");
File.WriteAllText(currentLogFile, sb.ToString());
Upvotes: 2
Views: 2636
Reputation: 919
Using suggestions from the answers, I've changed my code. It doesn't throw an exception anymore when first creating the file, but I'll mark an answer as Accepted when someone can explain why File.Create was throwing an IOException saying that the file was already in use and could not be accessed.
public static void logResults(System.Reflection.MethodBase method, Results result, string message)
{
string date = DateTime.Now.ToString();
int index = date.IndexOf(" ");
string subString = date.Substring(0, index);
string nwDate = Regex.Replace(subString, "/", "");
logFileName = "WebsiteRegressionProduction_TestCycle." + nwDate;
string currentLogFile = logFileLocation + @"\" + logFileName;
if (!File.Exists(currentLogFile))
{
File.WriteAllText(currentLogFile,
"DATE-TIME\tACTION\tTEST CLASS\tTEST NAME\tTEST STATUS\tERROR MESSAGES\n\n", Encoding.ASCII);
}
var sb = new StringBuilder();
sb.Append(String.Format("{0} : Test Executed: {1} : {2} : {3}\n\n", DateTime.Now.ToString(),
method.ReflectedType.Name, method, message));
using (var stream = File.AppendText(currentLogFile))
{
stream.Write(sb.ToString());
}
}
Upvotes: 1
Reputation: 666
File.Create returns FileStream, so you should be using a FileStream object like so:
FileStream fs = File.Create(currentLogFile);
Then do all your read/writes with the fs object.
Upvotes: 0
Reputation: 1626
1) Why do you read all previous content to append? 2) You can simply use log4net that do all that work for "free"
However, I suggest you to use a stream to have more control over the operation
FileStream fs = new FileStream(filePath, FileMode.Append);
byte[] data = Encoding.ASCII.GetBytes("The string you wanna append");
fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
The FileMode.Append open the specified (filePath) in Append if exists or create it if not.
Upvotes: 0
Reputation: 1118
File.AppendText is probably a much better option for what you are trying to accomplish. It will create the file if it is missing and add the text to the end. If you want to add the header to the start of the file, you'll just have to do a quick manual check to see if the file exists first.
Upvotes: 1