This project is read-only.
1
Vote

Add text qualifier to CSV files

description

I had an issue when opening the Blocked Process Details log file. Entries with embedded newline characters were causing the records to incorrectly display in Excel.
 
Ideally when writing out records, it is recommended to use text qualifers around fields containing the column or row delimiter. Below is a code I used to correct this:
 
ThreadFunction method in Form1.cs, replaced:
 
blocking.WriteLine(DateTime.Now + "," + blocked + "," + blocked_input_buffer + "," + spid + "," + spid_input_buffer);
 
with the following updated code:
 
string[] fields = new string[] { DateTime.Now.ToString(), blocked, blocked_input_buffer, spid, spid_input_buffer };
for (int i = 0; i < fields.Length; i++)
{
if (fields[i].Contains(",") || fields[i].Contains(Environment.NewLine))
    fields[i] = "\"" + fields[i].Replace("\"", "\"\"") + "\"";
}
blocking.WriteLine(String.Join(",", fields));

comments

glensmall wrote Sep 1, 2014 at 10:11 PM

Added to the list for Version 2.