Csharp/CSharp Tutorial/Development/Debug — различия между версиями

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

Версия 15:31, 26 мая 2010

Add Console.Out to TextWriterTraceListener

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

Add EventLogTraceListener to Debug.Listener

#define DEBUG
using System;
using System.Globalization;
using System.Diagnostics;
class MainClass
{
  public static void Main() 
  {
    TextWriterTraceListener tl = new TextWriterTraceListener("Example21_12.txt");
    Debug.Listeners.Add(tl);
    
    EventLogTraceListener t2 = new EventLogTraceListener("Application");
    Debug.Listeners.Add(t2);
    Debug.WriteLine("Starting Main()");
    Debug.Assert(1 == 2, "1 ==2");
    Debug.WriteLine("Exiting Main()");
    Debug.Flush();
    Debug.Close();
  }
}

Debug and Trace Output

// compile with: csc /r:system.dll file_1.cs
using System;
using System.Diagnostics;
class MyClass
{
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Debug.Assert(2 == 0, "Bad State");
    }
}
class MainClass
{
    public static void Main()
    {
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass();
        
        c.VerifyState();
        c.VerifyState();
    }
}

Debug Assert

using System;
using System.Diagnostics;
using System.Collections;
using System.Data;
using System.Text;
public class MainClass{
   public static void Main(){
     Debug.Assert(1 == 0,"Error!","Error: 1 == 0");
   }
}

Set up a TraceListener to a file

#define DEBUG
using System;
using System.Globalization;
using System.Diagnostics;
class MainClass
{
  public static void Main() 
  {
    TextWriterTraceListener tl = new TextWriterTraceListener("Example21_12.txt");
    Debug.Listeners.Add(tl);
    
    EventLogTraceListener t2 = new EventLogTraceListener("Application");
    Debug.Listeners.Add(t2);
    Debug.WriteLine("Starting Main()");
    Debug.Assert(1 == 2, "1 ==2");
    Debug.WriteLine("Exiting Main()");
    Debug.Flush();
    Debug.Close();
  }
}

Using Switches to Control Debug and Trace: BooleanSwitch

// compile with: csc /D:DEBUG /r:system.dll boolean.cs
using System;
using System.Diagnostics;
class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Debug.WriteLineIf(debugOutput.Enabled, "VerifyState Start");
        
        if (debugOutput.Enabled)
            Debug.WriteLine("VerifyState End");
    }
    
    BooleanSwitch debugOutput =  new BooleanSwitch("MyClassDebugOutput", "Control debug output");
    int i = 0;
}
class MainClass
{
    public static void Main()
    {
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass(1);
        
        c.VerifyState();
    }
}

Using Switches to Control Debug and Trace: TraceSwitch

// compile with: csc /r:system.dll file_1.cs
using System;
using System.Diagnostics;
class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Debug.WriteLineIf(debugOutput.TraceInfo, "VerifyState Start");
        
        Debug.WriteLineIf(debugOutput.TraceVerbose, "Starting field verification");
        
        if (debugOutput.TraceInfo)
        Debug.WriteLine("VerifyState End");
    }
    
    static TraceSwitch debugOutput =  new TraceSwitch("MyClassDebugOutput", "Control debug output");
    int i = 0;
}
class MainClass
{
    public static void Main()
    {
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass(1);
        
        c.VerifyState();
    }
}