With this code for a very basic logger:
lock (string.Concat("LogWritter_", this.FileName))
{
using (var fileStream = File.Open(this.FileName, FileMode.Append, FileAccess.Write, FileShare.Read))
{
using (var w = new StreamWriter(fileStream))
{
w.Write(message);
}
}
}
when I try it from a few threads simultaneously I quickly get the error:
The process can't access the file because its being used by another file.
Why the lock is not preventing the threads to access the file at the same time?
It doesn't matter if the threads call the same instance or different instances to the same file. Also I thought It could be because of some deferral when writing files in Windows, but on Linux happens the same thing.