Csharp/CSharp Tutorial/Development/TraceSource

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

Create TraceSource and SourceSwitch: switch level

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

public class MainClass
{
    public static void Main()
    {
        TraceSource ts = new TraceSource("MyTraceSource");
        ts.Listeners.Add(new ConsoleTraceListener());
        ts.Switch = new SourceSwitch("mySource");
        ts.Switch.Level = SourceLevels.Error;
        ts.TraceEvent(TraceEventType.Information, 0, "Information event");
        ts.TraceEvent(TraceEventType.Error, 0, "Error event");
    }
}

Create TraceSource and trace events

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

public class MainClass
{
    public static void Main()
    {
        Trace.Listeners.Add(new ConsoleTraceListener());
        TraceSource customerTraceSource = new TraceSource("YourClassName");
        Trace.WriteLine("Instantiated a new customer {0}", "asdf");
        Debug.Assert(1 == 2, "1 ==2");
        customerTraceSource.TraceEvent(TraceEventType.Warning, 1000,"data might be corrupt");
    }
}

Create TraceSource and Trace Information

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

public class MainClass
{
    public static void Main()
    {
        TraceSource myTraceSource = new TraceSource("myTraceSource");
        myTraceSource.TraceInformation("Customer successfully loaded from DB");
        myTraceSource.Flush();
        
        
    }
}