If you insist on reinventing the wheel and not use other parameters (log4net or memory mapped files), you will need to delay the record in the file itself and just try if you come across another thread that does the same thing.
A lightweight version using a task to obtain a non-blocking function and an indication as an example (not used as is).
public Task FlushLog(string filePath) { var task = new Task( () => while (true) { try { var file = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)); WriteLogToFileInTheUsualWay(file); file.Close(); file.Dispose(); break; } catch (UnauthorizedAccessException exception) { // Sleep randomly and try again Thread.Sleep(new Random(DateTime.Now.Milliseconds).Next(1000)); } } ); task.Start(); return task; }
source share