次の方法で共有


System.Diagnostics Tracing and DebugView

The other day I ran across DebugView by Sysinternals. After playing with it for a few I suddenly had a re-found love for System.Diagnostics.

“DebugView is an application that lets you monitor debug output on your local system, or any computer on the network that you can reach via TCP/IP. It is capable of displaying both kernel-mode and Win32 debug output, so you don’t need a debugger to catch the debug output your applications or device drivers generate, nor do you need to modify your applications or drivers to use non-standard debug output APIs.” from Sysinternals

This means you have a nice little exe you can carry on a thumb drive and collect those trace events at runtime on from any machine. Let’s look at some code.

First define a TraceSwitch. This switch gives you the ability to determine when to throw an event. You can define multiple switches.

public static TraceSwitch GeneralTraceSwitch = new TraceSwitch(”General”, “My Trace Switch”);

if ( GeneralTraceSwitch.TraceError )
Trace.TraceError( “YOUR ERROR MSG” );

Now let’s setup configuration. There are two major sections, listeners and switches. Listeners define where System.Tracing should send your events. The example below sends events to both the EventLog and a text file. Having said that has no influence on DebugView, you do not have to define a listener for DebugView to pick them up. The switches section defines the level of events to throw. 1 is the minimal number of events, while 4 is everything.

<configuration >
<system.diagnostics >

<trace autoflush=”true” indentsize=”0″ >
<listeners >
<add name=”EventLogTraceListener” type=”System.Diagnostics.EventLogTraceListener” initializeData=”Team Build”/>
<add name=”TextWriterTraceListener” type=”System.Diagnostics.TextWriterTraceListener” initializeData=”.\ci.log” />
</listeners >
</trace >

<switches >
<add name=”General” value=”4″ />
</switches >
 

</system.diagnostics >
</configuration >