I have a function that asynchronously writes to a log file, that is called multiple times. However, I'm getting an error:
System.IO.IOException: 'The process cannot access the file '<log path>' because it is being used by another process.'
The code is here:
public async void log(string msg)
{
await Task.Run(() => {
// Check that log directory exists, or create one
if (!Directory.Exists(@"log dir")) Directory.CreateDirectory(@"log dir");
// Append to log
using (StreamWriter w = File.AppendText(@"log path"))
{
w.WriteLine(DateTime.Now + " : " + msg);
w.Close();
}
});
}
My understanding of async programming comes mainly from node.js, which is a single-threaded language. Am I correct in thinking that since C# is multi-threaded (and takes a multi-threaded approach to async code) that IO doesn't automatically queue for a resource as it does in node.js?
Is there an easy way to write to files asynchronously in C#? Or am I better off just making this log function synchronous, since the performance cost would be irrelevant for a few line writes...