0

I'm new to c# and currently I'm working on error logging where in error logs will be placed into a text file. My dilemma now is that, I cannot format the text file nicely, I mean to make it easily readable. Can you help me on this? Thanks in advance. See below code.

Code for creating first row for headers

 if (!File.Exists(file))
 {
    file = new StreamWriter(file);
    builder.AppendFormat("{0, 10} {1, 25} {2, 30} {3, 30} {4, 30} {5, 50} ", "LOGTYPE", "DATE", "COMPLEXITY", "MESSAGE", "EXCEPTION TYPE", "STACK TRACE");
    builder.Append(Environment.NewLine);
 } //I got this right and looks good. 

Below code is my problem, I tried to put string format to set alignment but it doesn't work. The strings are still close to each other.

  builder.AppendFormat("{0, 10} {1, 25} {2, 35} {3, 45} {4, 55} {5, 65}", "Trace", DateTime.Now.ToString(), logDetails.Severity, logDetails.Message, logDetails.ExceptionType, logDetails.StackTrace);
शेखर
  • 17,412
  • 13
  • 61
  • 117
Moccassin
  • 169
  • 1
  • 4
  • 15
  • possible duplicate of [Inserting a tab character into text using C#](http://stackoverflow.com/questions/366124/inserting-a-tab-character-into-text-using-c-sharp) – JMK Oct 17 '13 at 09:03
  • It depends on the font used to display the output. Use a fixed width font. One where the width (in pixels) of letter 'i' is the same of the letter 'w' – Steve Oct 17 '13 at 09:10
  • @Steve how can I change based on your answer? Apologies I'm a newbie in programming. Thanks a lot – Moccassin Oct 17 '13 at 09:21
  • 1
    If you open your file using a texteditor like notepad and check if it is using a fixed width font, is your output text shown correctly? – Steve Oct 17 '13 at 09:27

1 Answers1

0

try something like:

string x = "{0, 10} {1, 25} {2, 35} {3, 45} {4, 55} {5, 65} Trace" 
           + ", " + DateTime.Now.ToString()+ ", " + logDetails.Severity+ ", " 
           + logDetails.Message,  logDetails.ExceptionType+ ", " 
           + logDetails.StackTrace;

Add + "\n" for newline.

शेखर
  • 17,412
  • 13
  • 61
  • 117
Mrinal Saurabh
  • 948
  • 11
  • 16