Csharp/CSharp Tutorial/Development/Trace Listener

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add ConsoleTraceListener to Trace

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

public class MainClass
{
    public static void Main()
    {
        Trace.Listeners.Add(new ConsoleTraceListener());
    }
}

Add DefaultTraceListener to Trace

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

public class MainClass
{
    public static void Main()
    {
        DefaultTraceListener dtl = new DefaultTraceListener();
        dtl.AssertUiEnabled = true;
        Trace.Listeners.Add(dtl);
    }
}

Add TextWriterTraceListener to Debug Listener

#define DEBUG
using System;
using System.Globalization;
using System.Diagnostics;
class MainClass
{
  public static void Main() 
  {
    TextWriterTraceListener tl = new TextWriterTraceListener(Console.Out);
    Debug.Listeners.Add(tl);
    Debug.WriteLine("Starting Main()");
    Debug.Assert(1 == 2, "1==2");
    Debug.WriteLine("Exiting Main()");
  }
}
Starting Main()
Fail: 1==2
Exiting Main()

Clear Trace Listener and Add EventLogTraceListener

using System;
using System.Diagnostics;
using System.Collections;
using System.Data;
using System.Text;
public class MainClass{
   public static void Main(){
     Trace.Listeners.Clear(); // clear all listeners
     Trace.Listeners.Add(new EventLogTraceListener("BugRiddenLog"));
     Debug.Assert(1 == 0,"Error!","Error: 1 == 0");
   }
}

Demonstrates debug output

#define DEBUG
using System;
using System.Globalization;
using System.Diagnostics;
class MainClass
{
  public static void Main() 
  {
    TextWriterTraceListener tl = new TextWriterTraceListener(Console.Out);
    Debug.Listeners.Add(tl);
    Debug.WriteLine("Starting Main()");
    Debug.Assert(1 == 2, "1==2");
    Debug.WriteLine("Exiting Main()");
  }
}
Starting Main()
Fail: 1==2
Exiting Main()

Post-deployment tracing

#define TRACE
using System;
using System.Globalization;
using System.Diagnostics;
class MainClass
{
  public static void Main() 
  {
    BooleanSwitch bsEnableTrace = new BooleanSwitch("TraceOutput", "Turn on tracing");
    TextWriterTraceListener tl = new TextWriterTraceListener("tract.txt");
    Trace.Listeners.Add(tl);
    EventLogTraceListener t2 = new EventLogTraceListener("Application");
    Trace.Listeners.Add(t2);
    Trace.WriteLineIf(bsEnableTrace.Enabled,"Starting Main()");
    if (bsEnableTrace.Enabled) 
    {
      Trace.Assert( 1 == 2, "1 == 2");
    }
    Trace.WriteLineIf(bsEnableTrace.Enabled, "Exiting Main()");
    Trace.Flush();
    Trace.Close();
  }
}

Write Trace based on the BooleanSwitch

#define TRACE
using System;
using System.Globalization;
using System.Diagnostics;
class MainClass
{
  public static void Main() 
  {
    BooleanSwitch bsEnableTrace = new BooleanSwitch("TraceOutput", "Turn on tracing");
    TextWriterTraceListener tl = new TextWriterTraceListener("tract.txt");
    Trace.Listeners.Add(tl);
    EventLogTraceListener t2 = new EventLogTraceListener("Application");
    Trace.Listeners.Add(t2);
    Trace.WriteLineIf(bsEnableTrace.Enabled,"Starting Main()");
    if (bsEnableTrace.Enabled) 
    {
      Trace.Assert( 1 == 2, "1 == 2");
    }
    Trace.WriteLineIf(bsEnableTrace.Enabled, "Exiting Main()");
    Trace.Flush();
    Trace.Close();
  }
}