问题
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