输出trace信息
The program below demonstrates output the trace to three different place, it's a very cool feature in .net, very convenient and exactly what we desire of most of the time.
1 eventlog
2 console
3 textfile
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace CSharpTest
{
class Program
{
static void Main(string[] args)
{
// a. Eventlog, need administrator priviledge
EventLog.WriteEntry("MyTestApp","Something wrong");
// b. Text File listener
TextWriterTraceListener textListener = new TextWriterTraceListener("log.txt");
Trace.Listeners.Add(textListener);
// c. Console listener
ConsoleTraceListener consoleListener = new ConsoleTraceListener();
Trace.Listeners.Add(consoleListener);
Trace.WriteLine("test trace");
Trace.Flush();
Trace.Close();
}
}
}