Nicely formatted text file

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));

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x