See Trace Listeners.
The DefaultTraceListener
will write to the Output window. To send messages to the TraceListener, you just need to call Trace.WriteLine("my output string");
.
// C#
System.Diagnostics.Trace.Listeners.Clear();
System.Diagnostics.Trace.Listeners.Add(
new System.Diagnostics.TextWriterTraceListener(Console.Out));
EDIT: In response to your comment, the following code will print "my output string" immediately before "Press enter to quit" in the console window:
static void Main(string[] args)
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
Trace.WriteLine("my output string");
Console.Write("Press enter to quit");
Console.ReadLine();
}
EDIT2:
After re-reading your question, I realised you're specifically trying to use the TraceSource
, in which case you just need to set up the Switch so it knows which level of trace events should be sent to the console window. Try adding this after you add your listeners:
SourceSwitch sourceSwitch = new SourceSwitch("SourceSwitch", "Verbose");
t.Switch = sourceSwitch;