Link to home
Start Free TrialLog in
Avatar of mitchellm44
mitchellm44

asked on

TraceSource usage in larger projects

I have encountered the idea of using the TraceSource in the John Robbins book, and have found a few references to the use of TraceSource, with one link indicating that you had to create a source fo each component.
I have tried to create a simple console app, that uses a console listener,
and create another class "myclass" that will be used in the console main app - where I will have one source
for the main app and another for the "myclass".  
I have tried adding the source in the config file and the switches set to all, and both have the #define TRACE in them. I can see the main console app trace fine. - Nothing in the "myclass".
I put statements into the constructor and a method, and have stepped through them, the statements are executed but no trace.  The tracing area is a learning area for me and I am sure that I am doing something dumb - but not sure what.
For my example I started with the code from this link

http://msdn2.microsoft.com/en-us/library/system.diagnostics.tracesource(VS.80).aspx

And then added the MyClass for an aggregate type usage.

#define TRACE //Required for TraceSource
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Security.Permissions;
namespace ResearchTrace
{
    // Initialize the trace source.
   
   
    public class MyClass
    {
        static TraceSource ts = new TraceSource("AggregateTrace");
        public MyClass()
        {
            Trace.TraceInformation("Aggregate Using Trace");
            ts.TraceInformation("We have Aggregate Constructor");
            ts.TraceEvent(TraceEventType.Information, 1, "AggregateTest TraceEvent 1");
            ts.Flush();
             
               
        }
        public void MyClassMethodTrace()
        {
            Trace.TraceInformation("AggregateMethodTrace Test");
            ts.TraceInformation("ContainedAggregateSource");
        }
    }
}


and the config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!--// The following configuration file can be used with this sample.
  // When using a configuration file #define ConfigFile.
  //-->
  <system.diagnostics>
     <sources>
        <source name="TraceTest">
        <listeners>
             <add name="console">
               <filter type="System.Diagnostics.SourceFilter"
                  initializeData="TraceTest"  />
             </add>
          </listeners>
         </source>
       
           <source name="AggregateTest">
             <listeners>
               <add name="console">
                 <filter type="System.Diagnostics.SourceFilter"
                  initializeData="AggregateTest"  />
             </add>
             </listeners>
           </source>
       
     </sources>
     <switches>
                    <!-- You can set the level at which tracing is to occur -->
                    <add name="TraceTest" value="All" />
                    <!-- You can turn tracing off -->
                    <!--add name="SourceSwitch" value="Off" -->
                   <add name="AggregateTest" value="All"/>
     </switches>
    <sharedListeners>
      <add name="console" type="System.Diagnostics.ConsoleTraceListener"/>
    </sharedListeners>
     <trace autoflush="true" indentsize="4"></trace>  
  </system.diagnostics>
</configuration>



Can I get the MyClass trace to be logged or did I misunderstand how this works?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Obvious question is "are you creating an instance of MyClass"?

Bob
Avatar of mitchellm44
mitchellm44

ASKER

Yes, at the end of the example code - I instantiate the class and use the method.
No logging.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just Prior to the point in the MS code where the READ is used to pause the Console..

ts.TraceEvent(TraceEventType.Suspend, 10, "Switch includes ActivityTracing, this should appear");
  ts.TraceEvent(TraceEventType.Critical, 11, "Switch includes Critical, this should appear");
  MyClass att = new MyClass();//That has a msg in the constructor
 att.MyClassMethodTrace(); // Do we see this?
  Console.WriteLine("Press any key to exit.");
  Console.Read();
Yes the MyClass creates a source called "AggregateTest" -- and now i see the error...

AggregateTrace and AggregateTest --- Thank you for making me look.

I knew it would be something dumb on my part.. Thank you very much!
I did not look at the names closely enough and the pointing out of the name hit the error in my logic. Very appreciated