1. Listener with configuration
app.config
<configuration>
<system.diagnostics>
<trace autoflush="false" indentsize="4">
<listeners>
<add name="myListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="TextWriterOutput.log" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace DiagnosticsTest
{
class Program
{
static void Main(string[] args)
{
TextWriterTraceListener listener1 = new TextWriterTraceListener(System.Console.Out);
Trace.Listeners.Add(listener1);
Trace.WriteLine("Trace Info");
Trace.Flush();
}
}
}
2. Listener without configure file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace TraceNoConfig
{
class Program
{
static void Main(string[] args)
{
Stream myFile = File.Create("TextFile.txt");
TextWriterTraceListener listener1 = new TextWriterTraceListener(myFile);
Trace.Listeners.Add(listener1);
}
}
}