Trace switches can be used in determine what to output to the log.

 

So in my libraries I will log different things at different levels. Error, 
Warning, Info.

Then when I need to debug something I can decide if I am just interested in one 
of these or all of these.

 

You can also have logic in your code based on trace switches.

 

    if (traceDetails.Switch.Level == SourceLevels.Information){

       //do this

    }

                

This can be useful if you are calling another web service and want to indicate 
to the web service to pass back debug information.

 

For example in my WebService Methods I can have a parameter called DebugLevel.

If tracing has been enabled then I pass the tracelevel value to the web service 
and have the web service return more debug information.

 

 

Regards

 

Adrian Halid 

 

From: [email protected] [mailto:[email protected]] On 
Behalf Of Greg Keogh
Sent: Friday, 8 January 2016 7:34 AM
To: ozDotNet <[email protected]>
Subject: Re: TraceSource and config

 

Got it working with these changes

 

BINGO! It wasn't working because I never specified a "switch" with "Verbose". I 
could never have guessed one was needed, as I thought it was to restrict the 
logging output and therefore I never bothered coding one. Perhaps it's in the 
fine print, but I'm no lawyer! So that's the magic trick that I must have 
consistently missed for years now. Much appreciated Dude -- Greg

 

P.S. It turns out the shared listener isn't needed in my case, so this reduced 
config section works:

 

<sources>
  <source name="MockSource" switchName="sourceSwitch" 
switchType="System.Diagnostics.SourceSwitch">
    <listeners>
      <add name="consListener" type="System.Diagnostics.ConsoleTraceListener"/>
      <add name="textfileListener" 
type="System.Diagnostics.TextWriterTraceListener" initializeData="mock.log" />
    </listeners>
  </source>
</sources>

<switches>
  <add name="sourceSwitch" value="Verbose"/>
</switches>

 

P.P.S. I also noticed you don't need the Flush before the Close.

Reply via email to