1

I have created a trace source in my code with a specific name, and then I want to use the section in app.config to attach a listener to it at runtime.

Here is my app.config:

  <system.diagnostics>
    <trace autoflush="true"/>
    <sources>
      <source name="myTraceSource"
              switchName="mySwitch"
              switchType="System.Diagnostics.SourceSwitch" >
        <listeners>
          <clear/>
          <add name="textwriterListener"
            type="System.Diagnostics.TextWriterTraceListener"
            initializeData="c:\dev\mylog.txt"
            traceOutputOptions="ProcessId, DateTime, Callstack" />
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="mySwitch" value="Verbose" />
    </switches>
  </system.diagnostics>

I can see that messages are being generated in the code with

this.TraceSource.TraceEvent(TraceEventType.Verbose, 0, p_message);

but nothing comes out in the log file (it isn't even created). When I set a breakpoint and look at this.TraceSource.Listeners, it is empty.

Any idea what I am doing wrong here, or any tips on debugging this sort of thing? Is it even possible to attach a new listener to an existing source like this?

I've also tried the following with no success:

 <system.diagnostics>
   <trace autoflush="true" indentsize="4">
     <listeners>
       <add name="TextListener" 
           type="System.Diagnostics.TextWriterTraceListener"
           initializeData="c:\dev\mylog.txt" />
       <remove name="Default" />
     </listeners>
   </trace>
 </system.diagnostics>
WildCrustacean
  • 5,896
  • 2
  • 31
  • 42
  • Haha, so turns out I'm an idiot, there was a `TraceSource.Listeners.Clear()` line somewhere in the program startup, which was removing any listeners that I added in the config file. It works now, I'm going to go ahead and accept the answer to this question. – WildCrustacean Sep 25 '12 at 19:17

1 Answers1

4

A possibility I see is if the directory you specify in the config file does not exist. That said you don't show how you initialize the TraceSource instance so I am not sure it wouldn't have something to do with that. In any case, a simple console application using your configuration section works fine.

class Program
{
    static TraceSource ts = new TraceSource("myTraceSource");

    static void Main(string[] args)
    {
        ts.TraceEvent(TraceEventType.Verbose, 0, "Hello");
    }
}

Also, make sure your app.config is complete. I assume you were only posting the System.Diagnostics section but here is the complete example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace autoflush="true"/>
    <sources>
      <source name="myTraceSource"
              switchName="mySwitch"
              switchType="System.Diagnostics.SourceSwitch" >
        <listeners>
          <clear/>
          <add name="textwriterListener"
            type="System.Diagnostics.TextWriterTraceListener"
            initializeData="c:\dev\mylog.txt"
            traceOutputOptions="ProcessId, DateTime, Callstack" />
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="mySwitch" value="Verbose" />
    </switches>
  </system.diagnostics>
</configuration>
blins
  • 2,515
  • 21
  • 32