I have a snippet of code that is writing to a memory stream in an asynchronous way (.NET Standard 2.1)

* Create a list of hot tasks, one per a line
* Then await when all of them will be finished
* Flush the writer buffer to the memoryStream at the end

Code:

```
await using var memoryStream = new MemoryStream();
await using var writer = new StreamWriter(memoryStream);

var recordTasks = stringRecordsToWrite.Select(r => writer.WriteLineAsync(r));

await Task.WhenAll(recordTasks);

await writer.FlushAsync();

var result = memoryStream.ToArray();

```

**Questions**

There are a couple of questions that bothering me:

1. There were reports that time to time some records had been skipped.
Thus, I wonder, could such implementation be the root cause. I've tried to reproduce the issue locally, but, unfortunately, no success
2. Also Resharper highlights that 'writer' (within SELECT statement) is a captured variable and disposed in outer scope. Can it be a problem?

Or those are false traces, and the implementation is fine, and I should try to found the reason out in other place

**P.s** The code snippet that, for attempt to reproduce the issue, but, as result a file about 250 MB size, and still no evidence of the issue
...

```
 internal class Program
    {
        public static async Task Main(string[] args)
        {
            var records = new Dictionary<string, IEnumerable<RecordsToWrite>>();

            for (var i = 0; i < 200; i++)
            {
                var recordKey = $"test-{i}";

                records.Add(recordKey, default);

                var itemRecords = new List<RecordsToWrite>();

                for (var x = 0; x < 500; x++)
                {
                    itemRecords.Add(new RecordsToWrite
                    {
                        Tracking = $"{recordKey}-Track-Ref-{x}"
                    });
                }

                records[recordKey] = itemRecords;
            }

            var resultAsBytes = new List<byte>();
            var randomizer = new Random();

            foreach (var kv in records)
            {
                await using var memoryStream = new MemoryStream();
                await using var writer = new StreamWriter(memoryStream);

                var recordsToWrite = kv.Value;

                var writingRecordsTasks = recordsToWrite.Select(x =>
                {
                    var randomLengthString = randomizer.Next(100, 5000);

                    return writer.WriteLineAsync($"track-ref-{x.Tracking}, " +
                                                 $"now in ticks: {DateTime.UtcNow.Ticks}, " +
                                                 $"content: {new string(GetRandomLetter(), randomLengthString)}");
                });

                await Task.WhenAll(writingRecordsTasks);

                await writer.FlushAsync();

                resultAsBytes.AddRange(memoryStream.ToArray());
            }

            var content = Encoding.UTF8.GetString(resultAsBytes.ToArray());
            await File.WriteAllLinesAsync("PathToAFile.txt", new[] { content });

            Console.ReadLine();
        }

        public static char GetRandomLetter() => (char)('a' + new Random().Next(0, 26));
    }

```