/// <summary> /// Shutdown and flush the Slack writer /// </summary> public void Dispose() { lock (Lock) { WriteTask.Wait(); } }
public void Flush() { if (WriteTask != null && WriteTask.IsCompleted == false) { WriteTask.Wait(); } File.AppendAllText(Filename, Buffer.ToString(), Encoding); }
public void WriteLine(string line, Encoding encoding) { //one thread at a time lock (LockObject) { if (Encoding == null) { Encoding = encoding; } else { //could be a mess otherwise if (Encoding != encoding) { throw new InvalidOperationException($"Multiple encodings detected while writting to file {Filename}"); } } //Wait until there are 64kb of data in the buffer before start writting and lock after that //if the write task is working Buffer.AppendLine(line); if (WriteTask?.IsCompleted ?? true) { if (Buffer.Length >= 64 * 1024) { WriteBuffer(); } else { //If it's less than 64kb in the buffer, create a task to automatically flush it after 2 seconds CreateFlushTask(); } } else { if (Buffer.Length > 64 * 1024) { //lock this thread until buffer has been emptied by writter WriteTask.Wait(); } } } }