现在的位置: 首页 > 综合 > 正文

输出trace信息

2012年11月06日 ⁄ 综合 ⁄ 共 867字 ⁄ 字号 评论关闭

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();

        }

    }

}

 

抱歉!评论已关闭.