3

I’m writing to a text file and it looks like this...

19/05/2010 15:33:34   Verbose Main    in main method
19/05/2010 15:33:34   Information DoesSomethingInteresting()  the answer to faster than light travel is

But what i’d like it to look like is...

19/05/2010 15:33:34   Verbose     Main                        in main method
19/05/2010 15:33:34   Information DoesSomethingInteresting()  the answer to faster than light travel is

You know, so it's all nicely formatted and tabbed aligned. Is there an easy way to do this, some function in the runtime that will deal with all the tedious padding?

Here is my code that does the writing

LogFile.Write(string.Format("{0}\t{1}\t{2}\t{3}", log.Time, log.Level, log.Method, log.Message));
Rob
  • 1,688
  • 4
  • 30
  • 43

1 Answers1

13

To align a string to the left use formatting pattern with comma (,) followed by a negative number of characters:

LogFile.Write(string.Format("{0,-10} {1,-11} {2,-30} {3}", ...));

For right alignment use a positive number.

dtb
  • 213,145
  • 36
  • 401
  • 431