views:

34

answers:

1

I have the following stringBuilder that I am using as part of Log4net log layout.

var sb = new StringBuilder(DateTime.Now.ToString(DateFormatString, CultureInfo.InvariantCulture));
            sb.Append("\t");
            sb.Append(log.EventAction);
            sb.Append("\t");
            sb.Append(log.Id);
            sb.Append("\t");
            sb.Append(log.MessageId);
            sb.Append("\t");
            sb.Append(log.CodeBlock);
            sb.Append("\t");
            sb.Append(log.Details);

I would expect the \t (tab) to give me an accurate tabbing as deliminator, so that when visually inspecting the text file, the columns would align. However this is not the case!

The columns are not aligning, and when I open up the text file in MS Word and "Display Special Characters", I see that the same tab entries seem to exist, but visually in notepad, columns don't align.

I'm really confused, maybe I need to review the definition of tab, can anyone provide insight or workaround to this issue?

Update:

I've noticed that if I have strings of identical length then the columns align, so for example:

string1(len5) TAB string2(len5) TAB string3(len5)
string1(len5) TAB string2(len5) TAB string3(len5)
string1(len5) TAB string2(len5) TAB string3(len5)

However if the strings differ in length, tab char is present, but strings don't align:

string1(len5) TAB string2(len5) TAB string3(len5)
string1(len10) TAB     string2(len5) TAB string3(len5)
string1(len5) TAB string2(len5) TAB string3(len5)
string1(len10) TAB     string2(len5) TAB string3(len5)

Something like this...

+2  A: 

A tab is just a logical separator. It has no magic ability to force the data to align, and it depends on how your viewer interprets tabs; it might mean "indent 4 characters", it might mean "pad rounding to the nearest 8th character". As such, it is normally very dependent on the existing data lengths. In Word it will usually be interpreted as "move to the next pre-defined tab", but that depends on a: what tabs are defined in Word, and b: how wide the data is already (i.e. what is "next").

In Excel (or any spreadsheet) it will generally be interpreted as columns. For this reason, a spreadsheet is probably your best option for browsing tabular delimited data (if you care about columns).

So; what viewer are you using?

Marc Gravell
Notepad.exe - often viewed on servers.
JL
@JL, then that simply isn't going to work. If you need data alignment you may need to pad with spaces instead, but for that to be feasible you need to know the maximum length of each field first.
Marc Gravell
padding ftw... otherwise a decent tabbed logviewer. Thanks Marc!
JL