Вопрос

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>
Это было полезно?

Решение

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>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top