Defining multiple TraceSources not running

风流意气都作罢 提交于 2019-12-04 06:06:16

问题


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("me@my-site.com");
        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.


回答1:


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>


来源:https://stackoverflow.com/questions/805154/defining-multiple-tracesources-not-running

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!