I just updated to xUnit 2.1. I'm using this method via a lot of tests running in parallels from differents assemblies to log to the same file with a certain name. The problem is I can't manage concurrency (even using TextWriter.Synchronized), every time I run my tests, different tests says:
The error is:
Message: System.IO.IOException : The process cannot access the file 'C:\LOGS\myFileName-20170510.txt' because it is being used by another process.
My method is:
private static void WriteToLogFile(string fileName, string strMessage)
        {
            DateTime utcNow = DateTime.UtcNow;
            lock (fileName)
            {
                using (StreamWriter w = File.AppendText(fileName + utcNow.Year + utcNow.Month.ToString("00") + utcNow.Day.ToString("00") + ".txt"))
                {
                    w.WriteLine("{0};{1};{2}", DateTime.UtcNow, SecurityHelper.GetPrincipalName(), strMessage);
                    w.Flush();
                    w.Close();
                }
            }
        }
I tried even (but this doesn't helped):
private static void WriteToLogFile(string fileName, string strMessage)
{
    DateTime utcNow = DateTime.UtcNow;
    lock (fileName)
    {
        using (StreamWriter w = File.AppendText(fileName + utcNow.Year + utcNow.Month.ToString("00") + utcNow.Day.ToString("00") + ".txt"))
        {
            w.WriteLine("{0};{1};{2}", DateTime.UtcNow, SecurityHelper.GetPrincipalName(), strMessage);
            w.Flush();
            w.Close();
        }
    }
}
private static readonly ConcurrentDictionary<string, object> Tokens = new ConcurrentDictionary<string, object>();
private static object GetToken(string fileName)
{
    if (!Tokens.ContainsKey(fileName))
        Tokens.TryAdd(fileName, new object());
    return Tokens[fileName];
}
This is due to some strange behaviour of xUnit? Thanks in advance.
 
     
    