Using BlockingCollection to implement producer and consumer queues and write text

Recently, several small projects have been developed. The results need to be written into a txt file and divided into files according to the time. Due to the high efficiency requirements, the producer and consumer models are used to write the text. The thread only needs to add a queue and return immediately, without waiting for the time to write the file

public class WriteItem : IDisposable
    {
        public string Filename { get; }
        public Encoding Encode { get; }
        public bool Append { get; }
        public bool TimeName { get; }

        private StreamWriter _writer;

        private readonly BlockingCollection<string> _blocking = new BlockingCollection<string>();

        public void Write(string msg)
        {
            _blocking.Add(msg);
        }

        public void WriteLine(string msg)
        {
            Write(msg + Environment.NewLine);
        }

        public WriteItem(string filename, Encoding encode, bool append = true, bool timeName = false)
        {
            Filename = filename;
            Encode = encode;
            Append = append;
            TimeName = timeName;

            if (timeName && string.IsNullOrEmpty(Path.GetExtension(filename)))
            {
                this.Filename = Path.Combine(this.Filename, DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
            }
            var dir = Path.GetDirectoryName(this.Filename);
            Directory.CreateDirectory(dir ?? throw new InvalidOperationException());
            _writer = new StreamWriter(this.Filename, this.Append, this.Encode) { AutoFlush = true };
            Task.Factory.StartNew(() =>
            {
                foreach (var s in _blocking.GetConsumingEnumerable())
                {
                    if (TimeName)
                    {
                        var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
                        var nowDay = DateTime.Now.ToString("yyyy-MM-dd").ToString();
                        if (fileNameWithoutExtension != nowDay)
                        {
                            _writer.Dispose();
                            _writer = new StreamWriter(this.Filename, this.Append, this.Encode) { AutoFlush = true };
                        }
                    }
                    _writer.Write(s);
                }

            }, TaskCreationOptions.LongRunning);
        }

        public void Dispose()
        {
            _writer?.Dispose();
            _blocking?.Dispose();
        }
    }

Then write a dictionary to maintain:

 public class FileWriteQueue
    {
        private static readonly Dictionary<string, WriteItem> Dictionary = new Dictionary<string, WriteItem>();

        public static void AddOrUpdate(string key, WriteItem item)
        {
            if (Dictionary.ContainsKey(key))
            {
                Dictionary[key].Dispose();
                Dictionary[key] = item;
            }
            else
            {
                Dictionary.Add(key, item);
            }
        }

        public static WriteItem Get(string key)
        {
            return Dictionary[key];
        }
    }

In actual use, add WirteItem and set the output directory:

            FileWriteQueue.AddOrUpdate("success",new WriteItem(Path.Combine("Result","Success"),Encoding.Default,true,true));
            FileWriteQueue.AddOrUpdate("error", new WriteItem(Path.Combine("Result", "fail"), Encoding.Default, true, true));
            for (int i = 0; i < 1000; i++)
            {
                FileWriteQueue.Get("success").WriteLine(i.ToString());
            }

Tags: C# encoding

Posted on Thu, 30 Apr 2020 19:40:04 -0700 by peranha