这可能需要更多工作,但我会反其道而行之。
TraceListener为控制台实例化一个,为日志文件实例化一个;之后
Trace.Write,请在代码中使用语句,而不要使用
Console.Write。之后,删除日志或控制台输出或附加其他日志记录机制变得更加容易。
static void Main(string[] args){ Trace.Listeners.Clear(); TextWriterTraceListener twtl = new TextWriterTraceListener(Path.Combine(Path.GetTempPath(), AppDomain.CurrentDomain.FriendlyName)); twtl.Name = "TextLogger"; twtl.TraceOutputOptions = TraceOptions.ThreadId | TraceOptions.DateTime; ConsoleTraceListener ctl = new ConsoleTraceListener(false); ctl.TraceOutputOptions = TraceOptions.DateTime; Trace.Listeners.Add(twtl); Trace.Listeners.Add(ctl); Trace.AutoFlush = true; Trace.WriteLine("The first line to be in the logfile and on the console.");}据我所知,您可以在应用程序配置中定义侦听器,从而可以在不触摸构建的情况下激活或停用日志记录。



