[SOLVED] What is a fast async text writer that can be used to log a line of text to a csv file?

Issue

I am trying to find a text writer that will not block it’s calling thread. It will be called over 400k times a day and will be used for logging lines of text to a csv file. Here is what I have come up with so far:

public void callWriter()
        {

            textwriter.WriteToFileAsync(LINE_OF_DATA);

            //continue with rest of my program while file gets written to
        }

public async void WriteToFileAsync(string data)
        {
            using (StreamWriter streamWriter =
                new StreamWriter
                (
                    _path,
                    APPEND_FILE,
                    System.Text.Encoding.UTF8,
                    BUFFER_SIZE_IN_BYTES
                ))
            {
                await streamWriter.WriteLineAsync(data);
            }
        }

Solution

Based on clarifying your need is for logging in the comments, I would suggest using NLog.

Here’s a SO post on configuring NLog, for which the question is important, since any buffer for logging is (as others have stated) a queue, which needs to be sized accordingly, otherwise it ‘overflows’ and messages can be lost.

It is better not to rely on rolling your own solution here. Projects that have been through several public iterations of bug-fixes and dealing with all sorts of scenarios will be more robust and be better performing.

How to configure NLog programmatically for Async target?

You can find performance comparisons of NLog to other .NET logging frameworks/libraries and see that NLog is fastest.

Answered By – majixin

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *