Use Trace. It is designed to do what you need.
using System;
using System.Diagnostics;
class Test
{
static void Main()
{
Trace.Listeners.Add(new TextWriterTraceListener("yourlog.log"));
Trace.AutoFlush = true;
Trace.Indent();
Trace.WriteLine("Entering Main");
Console.WriteLine("Hello World.");
Trace.WriteLine("Exiting Main");
Trace.Unindent();
Trace.Flush();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…