tags:

views:

164

answers:

1

I'm new with TraceSource so I'm doing some investigation into how it can/ can't be used (basically pros and cons).

Something I do like is that I can get dumps from within the .NET framework itself, so I've made a little app to test that and using my own custom source together (as that's how I'd expect it to be used), like so:

class Program
{
    static void Main(string[] args)
    {
        SmtpClient smtp = new SmtpClient();
        var mm = new MailMessage();
        mm.To.Add("[email protected]");
        mm.Subject = "Trace Testing";
        smtp.Send(mm);

        var ts = new TraceSource("MyCustomTracer");

        ts.TraceEvent(TraceEventType.Error, 0, "This is an error");
        ts.TraceEvent(TraceEventType.Information, 0, "Just debugging now");
    }
}

I've then added some listeners into the App.config like this:

<system.diagnostics>
<trace autoflush="true" />
<sources>
  <source name="MyCustomTracer"
          switchValue="Information, ActivityTracing">
    <listeners>
      <add name="sdt"
          type="System.Diagnostics.XmlWriterTraceListener"
          initializeData= "traceOutput.log" />
    </listeners>
  </source>
  <source name="System.Net"
          switchValue="Information, ActivityTracing, Critical">
    <listeners>
      <add name="sdt"
          type="System.Diagnostics.XmlWriterTraceListener"
          initializeData= "traceOutput.log" />
    </listeners>
  </source>
</sources>
</system.diagnostics>

But for some reason when I run the app the 2 events I'm logging via MyCustomTracer aren't going into the log file unless I comment out the SmtpClient stuff (ie - only have my custom tracer used).

I would have expected that multiple TraceSources can be used in the manner in which I'm trying to use them, I'm just not sure what's going wrong.

A: 

Found the problem, a complete noob mistake, both my TraceSource items have a Listener which is writing to the same file. Although I'm not sure exactly the error, but it'd be some kind of clash when writing.

If you want to have multiple sources using the same listener you need to use the <sharedListeners /> like this:

<system.diagnostics>
<trace autoflush="true" />
<sources>
  <source name="MyCustomTracer"
          switchValue="Information, ActivityTracing">
    <listeners>
       <add name="sdt" />
    </listeners>
  </source>
  <source name="System.Net"
          switchValue="Information, ActivityTracing, Critical">
    <listeners>
      <add name="sdt" />
    </listeners>
  </source>
</sources>
<sharedListeners>
    <add name="sdt"
        type="System.Diagnostics.XmlWriterTraceListener"
        initializeData= "traceOutput.log" />
</sharedListeners>
</system.diagnostics>
Slace