C# `Trace.WriteLine` to print to file only that temporarily skips console ouput print

时间:2016-02-03 04:01:48

标签: c# logging command-line

As we discussed here, we can use Trace.WriteLine to write logs to both command-line console and a chosen file.

My need is to sometimes use Trace.WriteLine to print ONLY to file, NOT to console.

How can we reach that?

p.s.

Here is how I init the listener

#region init logging
Trace.Listeners.Clear();

var logFile = Path.Combine("Path", "to", "log file");
var twtl = new TextWriterTraceListener(logFile);
twtl.Name = "TextLogger";
twtl.TraceOutputOptions = TraceOptions.ThreadId | TraceOptions.DateTime;
var 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.");
#endregion

0 个答案:

没有答案