How to format text file with tab to make it readable and nice look

最后都变了- 提交于 2019-12-11 14:58:10

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/19422252/how-to-format-text-file-with-tab-to-make-it-readable-and-nice-look

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!