Csharp/C Sharp by API/System.Diagnostics/Trace

Материал из .Net Framework эксперт
Версия от 12:13, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Trace.Assert

  
using System;
using System.IO;
using System.Diagnostics;
class MainClass
{
  [STAThread]
  static void Main(string[] args)
  {
    int i = 0;
    Trace.Assert((i == 1), "My Trace Assertion");
  }
}


Trace.Listeners

  
using System;
using System.Diagnostics;
namespace nsEventLogs
{
    public class LogTrace
    {
        static public void Main ()
        {
            EventLog EvLog = new EventLog ();
            if (!EventLog.SourceExists ("AppLog.exe"))
            {
                Console.WriteLine ("Creating event log source");
                EventLog.CreateEventSource ("AppLog.exe", "Application");
            }
            EvLog.Source = "AppLog.exe";
            Trace.Listeners.Add (new EventLogTraceListener (EvLog));
            Trace.Listeners.Add (new TextWriterTraceListener (Console.Out));
            Trace.WriteLine ("Debugging to the event log");
            EvLog.Source = "AppLog.exe";
        }
    }
}


Trace.WriteLine

  
using System;
using System.Diagnostics;
namespace nsEventLogs
{
    public class LogTrace
    {
        static public void Main ()
        {
            EventLog EvLog = new EventLog ();
            if (!EventLog.SourceExists ("AppLog.exe"))
            {
                Console.WriteLine ("Creating event log source");
                EventLog.CreateEventSource ("AppLog.exe", "Application");
            }
            EvLog.Source = "AppLog.exe";
            Trace.Listeners.Add (new EventLogTraceListener (EvLog));
            Trace.Listeners.Add (new TextWriterTraceListener (Console.Out));
            Trace.WriteLine ("Debugging to the event log");
            EvLog.Source = "AppLog.exe";
        }
    }
}


Trace.WriteLineIf

  
using System;
using System.Diagnostics;
class TracingExample
{
  static void Main(string[] args)
  {
    TraceSwitch General = new TraceSwitch("General", "Application Switch");
    Trace.WriteLineIf(General.TraceError, "General - Error Tracing Enabled");
    Trace.WriteLineIf(General.TraceWarning, "General - Warning Tracing Enabled");
    Trace.WriteLineIf(General.TraceInfo, "General - Info Tracing Enabled");
    Trace.WriteLineIf(General.TraceVerbose, "General - Verbose Tracing Enabled");
  }
}

/*
<?xml version="1.0" encoding="utf-8" ?> 
<configuration>
    <system.diagnostics>
        <switches>
            <add name="General" value="1" />
        </switches>
    </system.diagnostics>
</configuration>
*/