Writing to file using TPL with ConcurrentQueue Here is a sample code to explain the approach I am going for. 

public class LogWriter
{
    #region Private Members

    // If writer is not static class, still need to keep single message list; same for other members
    private static ConcurrentQueue&lt;string&gt; _logMessages = new ConcurrentQueue&lt;string&gt;();
    private static object locker = new object();
    private static bool _stopAfterCurrentQueue = false;
    private static bool _discardQueueAndStop = false;

    private static CancellationTokenSource _tokenSource = new CancellationTokenSource();
    #endregion

    public void Write(string text)
    {
        if (!_stopAfterCurrentQueue &amp;&amp; !_discardQueueAndStop)
        {
            Task.Run(() =&gt;
           {
               _logMessages.Enqueue(text);

           });

            Task.Run(() =&gt;
            {
                while (!_logMessages.IsEmpty)
                {
                    foreach (var item in _logMessages)
                    {

                        _logMessages.TryDequeue(out string current);
                        lock (locker)
                        {
                            // Will be replaced by StreamWriter
                            File.AppendAllText("Log_" + DateTime.Now.ToString("yyyyMMMdd") + ".txt", current + Environment.NewLine);
                        }
                    }
                }
            }, _tokenSource.Token);
        }
    }

    public void ProcessCurrentAndStop()
    {
        // Only stops accepting new messages, will process the current queue
        _stopAfterCurrentQueue = true;
    }

    public void DiscardQueueAndStop()
    {
        // Cancels subsequent Enqueue
        _tokenSource.Cancel();

        // No more writing even if there is something in the queue
        _discardQueueAndStop = true;
    }

    public void RestartLogging()
    {
        _stopAfterCurrentQueue = false;
        _discardQueueAndStop = false;

        _tokenSource.Dispose();
        _tokenSource = new CancellationTokenSource();
    }
}


The idea is to write to file in asynchronous way. I am trying to make it thread safe as well. There is not much to process here except to write message to a file. 

I would like to understand what could be potential issues with this approach and also if it would be better to keep class static.
Let’s try to get rid of ConcurrentQueue. And Task.Run – those are expensive ones. It also makes sense to use async file access – which is a way more lightweight.

public class LogWriter
{
    public static LogWriter Instance = new LogWriter();

    LogWriter()
    {
        Enabled = true;
        Cts = new CancellationTokenSource();
        Task = Task.CompletedTask;
    }

    bool Enabled { get; set; }
    CancellationTokenSource Cts { get; set; } 
    Task Task { get; set; }
    string Path =&gt; $"Log_{DateTime.Now:yyyyMMMdd}.txt";

    public void Start() =&gt; Enabled = true;

    public void Stop(bool discard = false)
    {
        Enabled = false;
        if (discard)
        {
            Cts.Cancel();
            Cts = new CancellationTokenSource();
            Task = Task.ContinueWith(t =&gt; { });
        }
    }

    public void Write(string line) =&gt;
        Write(line, Path, Cts.Token);

    void Write(string line, string path, CancellationToken token)
    {
        if (Enabled)
            lock(Task)
                Task = Task.ContinueWithAsync(
                    t =&gt; AppendAllTextAsync(path, line + NewLine, token),
                    token);
    }        
}


Where missing part would be:

static class AsyncContinuations
{
    public static async Task ContinueWithAsync(this Task task, Func&lt;Task, Task&gt; continuationFunction, CancellationToken cancellationToken)
    {
        await task;
        cancellationToken.ThrowIfCancellationRequested();
        await continuationFunction(task);
    }
}