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

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

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

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 to a file

using System;
using System.IO;
using System.Diagnostics;
class MainClass
{
  [STAThread]
  static void Main(string[] args)
  {
    FileStream Log = new FileStream("Log.txt", FileMode.OpenOrCreate);
    Trace.Listeners.Add(new TextWriterTraceListener(Log));
    Trace.WriteLine("My Trace String To Log File");
    Trace.Flush();
    Log.Close();
  }
}

Trace to console

using System;
using System.IO;
using System.Diagnostics;
class MainClass
{
  [STAThread]
  static void Main(string[] args)
  {
    Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
    Trace.WriteLine("My Trace to the console");
    
    }
}

Tracing To Debugger

using System;
using System.IO;
using System.Diagnostics;
class MainClass
{
  [STAThread]
  static void Main(string[] args)
  {
    Trace.WriteLine("My Trace String");
    Trace.Flush();
  }
}

Tracing To EventLog

using System;
using System.IO;
using System.Diagnostics;
class MainClass
{
  [STAThread]
  static void Main(string[] args)
  {
    //You can change the listener with the following code
    EventLogTraceListener EventListener = new EventLogTraceListener("MyApp");
    Trace.Listeners.Add(EventListener);
    
    Trace.WriteLine("My Trace String To Console");
    
    }
}

Use Trace Switch

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>
*/

Use Trace Switch defined in config file

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

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