定义不运行多个TraceSources多个、定义、TraceSources

2023-09-03 08:16:25 作者:我姓曲也娶不了你

我是新与TraceSource所以我做了一些调查,如何能/不能使用(基本优点和缺点)。

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

这是我不喜欢的是,我可以在.NET框架本身中得到转储,所以我做了一个小应用程序来测试,并用自己的自定义源一起(因为这就是我希望它是使用),像这样:

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");
    }
}

然后,我增加了一些听众到的App.config 是这样的:

<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>

但是,当我运行的应用程序的两个事件,我通过 MyCustomTracer 登录某些原因不打算到日志文件,除非我注释掉 SmtpClient 的东西(也就是 - 只用我的自定义示踪剂)。

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).

我本来期望多个TraceSources可以在其中我试图使用它们的方式使用,我只是不知道发生了什么错。

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.

推荐答案

发现了问题,一个完整的小白的错误,我的两个TraceSource项目都被写入同一个文件监听器。虽然我不知道确切的错误,但它写的时候会是某​​种冲突。

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.

如果你想使用,你需要使用相同的监听器有多个来源的&LT; sharedListeners /&GT; 是这样的:

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>