Trace.WriteLineIf() is great for filtering detail by level.  For example,
if you had a state variable that held the current trace detail level
(called traceDetailLevel of type TraceSwitch) you could do the following:

TraceSwitch ts = new TraceSwitch("tracing", "description");
// ...
Trace.WriteLineIf(ts.TraceInfo, "attempting action 'x'...");
// ...
Trace.WriteLineIf(ts.TraceError, "Error performing 'y'!");
// ...
Trace.WriteLineIf(ts.TraceWarning, "Warning action 'x' defaulting to...");
// ...
Trace.WriteLineIf(ts.TraceVerbose, "I think you ought to know I'm feeling
very depressed.");

traceDetailLevel would be tied to an optional entry in the app.config.  If
you wanted to display just errors your app.config would look something
like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <system.diagnostics>
  <switches>
   <add name="tracing" value="1"/>
  </switches>
 </system.diagnostics>
</configuration>

To bump it up to errors and warnings change the switch value from 1 to 2:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <system.diagnostics>
  <switches>
   <add name="tracing" value="2"/>
  </switches>
 </system.diagnostics>
</configuration>

1 = Error or worse, 2 = Warning or worse, 3 = Info or worse, 4 = Verbose
or worse, and 0 = Off.

If you wanted trace output to be sent to "trace.log" your app.config might
look like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <system.diagnostics>
  <trace autoflush="true" indentsize="0">
   <listeners>
    <add name="MyListener"

 type="System.Diagnostics.TextWriterTraceListener"
     initializeData="trace.log"/>
   </listeners>
  </trace>
  <switches>
   <add name="tracing" value="2"/>
  </switches>
 </system.diagnostics>
</configuration>


In all cases, in order for this to occur the user simply edits the
app.config file and re-runs the application.  You application would
already support multi-level tracing to a log file with as little as two
lines of code (creation of a TraceSwitch object and call to
Trace.WriteLineIf(ts.Trace{xxx}, ...)

This architecture is very flexible.  If you want production code to do
something special with Trace.Assert() (other than write the message to the
log file) simply derive a class from TraceListener and override Fail().

The only work is deciding what to put in and where to put Trace.Write[Line]
If() calls.

http://www.peterRitchie.com/

On Thu, 25 Aug 2005 10:06:04 -0500, Franklin Gray
<[EMAIL PROTECTED]> wrote:

>From what I can see from the help files, this trace stuff is just a fancy
>way of writing to data to a file (if that's the listener I choose) if a
>flag is set and I can't see any advantage of it over standard logging to
a
>file if a flag is set.  I assume I'm wrong because I can't see MS
spending
>a lot of time on this for no reason.  So what am I missing?
>
>
>
>Message from Frans Bouma <[EMAIL PROTECTED]>@DISCUSS.DEVELOP.COM
received
>on 08/25/2005 09:30 AM
>
>08/25/2005 09:30 AM
>
>
>
>Frans Bouma <[EMAIL PROTECTED]>@DISCUSS.DEVELOP.COM
>
>Please respond to "Unmoderated discussion of advanced .NET topics."
><[email protected]>
>Sent by "Unmoderated discussion of advanced .NET topics."
><[email protected]>
>
>
>
>        To:     [email protected]
>        cc:
>        Subject:        Re: [ADVANCED-DOTNET] Writing code for Debugging
>
>> I've been given the task of coming up with a standard for our
>> new app for help in debugging the code when a client calls in
>> and has a problem.  So far, all I can think of is to log all
>> variable values at the beggening of all functions and logging
>> the output of all functions.  Also logging all screen data
>> (if grid, only current row).
>>
>> Questions:
>>
>> 1) What have you done in the past to help debugging?
>>
>> 2) Is there a better way then my idea above?
>
>        Yes, look into trace switches and listeners. You can make life
>easier with a library like Log4Net for example which has a
>lot of different fine grained trace listeners on board. The main thing is
>that trace switches can be turned on or off with a .config
>file, and the output receiver can also be configured in the config file.
>This means that you don't have to recompile a build for
>your customer to enable logging of data at runtime.
>
>        Enabling tracing is very simple, the .NET documentation has a lot
>of info on this (tracing, trace listeners, trace
>switches). Also log4net has extra documentation if you want to use
log4net
>for this (you can use the default .NET build in tracing)

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to