I have a function to write log to a text file:
using (var mutex = new Mutex(false, fullPath.Replace("\", "")))
{
mutex.WaitOne(Timeout.Infinite, false);
File.AppendAllText(fullPath, content.ToString());
mutex.ReleaseMutex();
}
The function is called from multiple places, so I use Mutex to make sure only 1 process is accessing the file.
It was working perfectly until I upgraded to .Net 8, I hit the error “The process cannot access the file because it is being used by another process.” intermittently. Looks like the file is not being released sometimes.
It only happen when I deploy to Azure App Service, it is working fine in my local. Anyone please help…
I thought the AppendAllText is not releasing the file, so I use StreamWriter
using (var mutex = new Mutex(false, fullPath.Replace("\", "")))
{
mutex.WaitOne(Timeout.Infinite, false);
using (var writer = new StreamWriter(fullPath, true))
{
writer.WriteLine(content.ToString());
writer.Flush();
}
mutex.ReleaseMutex();
}
But I still hit the same error.
Jimmy Koo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.