By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,900 Members | 1,453 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,900 IT Pros & Developers. It's quick & easy.

Tracing with TraceSource to Output (Console Window)

P: 1
One of the features offered by .NET 2.0 is the use of the TraceSource class. In an attempt to demonstrate its use, I wanted to run my test under the following conditions:

1. Use TraceSource class (Not Trace or Debug classes)

2. Set up the trace solution in the application's application.config file

3. Use Visual Basic.NET to test the trace

4. Test the TraceSource using Visual Studio.NET 2005 IDE "Windows application" rather than with a "Console application"

5. Use "Verbose" level

6. Direct all trace messages to the IDE's Output (Console Window)

7. I wanted to first test the traceSource solution using Visual Basic.NET before attempting it with C#.


Config File Content:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.   <system.diagnostics>
  4.     <sources>
  5.       <source name="TestApp"
  6.        switchName="sourceSwitch"
  7.        switchType="System.Diagnostics.SourceSwitch">
  8.         <listeners>
  9.           <add name="console"
  10.            type="System.Diagnostics.ConsoleTraceListener">
  11.           </add>
  12.           <remove name="Default"/>
  13.         </listeners>
  14.       </source>
  15.     </sources>
  16.     <switches>
  17.       <add name="sourceSwitch" value="Verbose"/>
  18.     </switches>
  19.   </system.diagnostics>
  20. </configuration>

Simple Source Code:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Diagnostics
  3. Public Class Form1
  5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  6.         method1()
  7.         method2()
  8.         method3()
  9.     End Sub
  11.     Private Sub method1()
  12.         Console.WriteLine("Inside Method1")
  13.     End Sub
  16.     Private Sub method2()
  17.         Console.WriteLine("Inside Method2")
  18.     End Sub
  21.     Private Sub method3()
  22.         Console.WriteLine("Inside Method3")
  23.     End Sub
  25. End Class

At issue:

I Wanted to see autogenerated trace events identifying each method call in my application as they were being processed. I assume that Trace Level "Verbose " will show this, however,I do not see any trace statements in the Output window. I see the Console.Writeline statements that I've inserted, but I do not see anything generated by the TraceSource setup. Am I assuming too much from Tracesource? I believe that I was also suppose to add a #define TRACE preprocessor directive (for C#) at the beginning of my source code to turn Tracking on, however, I'm not sure how/where to do the same with Visual Basic.NET.

Thank you,

Sep 3 '08 #1
Share this Question
Share on Google+
1 Reply

P: 1
Hi mquincey,

You may have already solved your issue, but I faced the same thing. Change the ConsoleTraceListener to DefaultTraceListener. You'll now see your messages in the output window.

I'm not sure why the Console listener does not have the same effect, it confused me too.

By the way, TraceSources are a fantastic feature. What I've done is have each component assembly use a trace source with the same name as the assembly. Therefore, if I want to just trace one assembly in a production environment, I just have to add the trace source config with the assembly name.
Oct 5 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.