简体   繁体   English

格式很好的文本文件

[英]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));

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. 对于右对齐,请使用正数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM