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

        }

    }

}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-15
  • 2021-11-20
  • 2021-06-28
  • 2021-06-09
  • 2021-07-03
猜你喜欢
  • 2021-04-29
  • 2021-05-18
  • 2022-12-23
  • 2021-07-24
  • 2022-02-23
  • 2022-12-23
相关资源
相似解决方案