使用多个跟踪侦听器

2024-02-19

我有 2 个 WCF 服务,它们是从单个 Windows 主机托管的。我使用跟踪侦听器将数据记录到应用程序日志中。我已将以下代码添加到配置文件中。

<system.diagnostics>
<switches>
  <add name="ReaderService.Switch" value="4"/>
  <add name="InfoService.Switch" value="4"/>
</switches>
<trace autoflush="false" indentsize="4">
  <listeners>
    <add name="EventLogTraceListener"
      type="System.Diagnostics.EventLogTraceListener"
      initializeData="ReaderServiceLog" />
  </listeners>
</trace>
</system.diagnostics>

来自这两个服务的所有日志都显示在源 ReaderServiceLog 名称下。我想要做的是,每个服务的日志应该显示在不同的源名称下。

例如,来自 ReaderService 的日志应显示在名称 ReaderServiceLog 下,来自 InfoService 的日志应显示在 InfoServiceLog 下。我修改了我的配置,如下所示。

<system.diagnostics>
<switches>
  <add name="ReaderService.Switch" value="4"/>
  <add name="InfoService.Switch" value="4"/>
</switches>
<sources>
  <source name="EventLogTraceListener">
    <listeners>
      <add name="EventLogTraceListener"
      type="System.Diagnostics.EventLogTraceListener"
      initializeData="ReaderServiceLog" />
    </listeners>
  </source>
  <source name="InfoService">
    <listeners>
      <add name="EventLogTraceListener"
      type="System.Diagnostics.EventLogTraceListener"
      initializeData="InfoServiceLog" />
    </listeners>
  </source>
</sources>
</system.diagnostics>

并使用了这段代码:

private TraceSource ts = new TraceSource("InfoService");
ts.TraceInformation(outputMessage, aslErrorText);
ts.Flush();

但这不起作用。它根本不记录任何内容。

我也尝试过this https://stackoverflow.com/questions/805154/defining-multiple-tracesources-not-running?lq=1。但这不起作用。

<system.diagnostics>
<switches>
  <add name="ReaderService.Switch" value="4"/>
  <add name="InfoService.Switch" value="4"/>
</switches>
<sources>
  <source name="ReaderService"
          switchValue="Information, ActivityTracing">
    <listeners>
      <add name="EventLogTraceListener"/>
    </listeners>
  </source>
  <source name="InfoService"
          switchValue="Information, ActivityTracing">
    <listeners>
      <add name="EventLogTraceListener"/>               
    </listeners>
  </source>
</sources>
<sharedListeners>
  <add name="EventLogTraceListener"
       type="System.Diagnostics.EventLogTraceListener"
       initializeData="ServiceLog" />
</sharedListeners>

我使用了与上面相同的 C# 代码。此代码可以正确执行日志记录,但同样,这两个服务的名称相同。即服务日志。

我在这里错过了什么吗?或者还有其他办法吗? 请帮忙


此配置添加了 2 个不同的跟踪源(使用文件侦听器;如果您愿意,您可能需要更改侦听器和目录路径):

<?xml version="1.0"?>
<configuration>
  ...
  <system.diagnostics>
    <switches>
      <add name="data" value="All" />
      <add name="error" value="All" />
    </switches>
    <sources>
      <source name="DataSource" switchName="data" switchType="System.Diagnostics.SourceSwitch">
        <listeners>
          <clear />
          <add name="dataListener" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
             initializeData="FileLogWriter"
             Append="true"
             AutoFlush="true"
             BaseFileName="data"
             CustomLocation="D:\Log\App\Data"
             DiskSpaceExhaustedBehavior="DiscardMessages"
             Encoding="Unicode"
             IncludeHostName="false"
             LogFileCreationSchedule="Daily"
             location="Custom"
             MaxFileSize="900000000000" />
        </listeners>
      </source>
      <source name="ErrorSource" switchName="error" switchType="System.Diagnostics.SourceSwitch">
        <listeners>
          <clear />
          <add name="errorListener" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
             initializeData="FileLogWriter"
             Append="true"
             AutoFlush="true"
             BaseFileName="error"
             CustomLocation="D:\Log\App\Error"
             DiskSpaceExhaustedBehavior="DiscardMessages"
             Encoding="Unicode"
             IncludeHostName="false"
             LogFileCreationSchedule="Daily"
             location="Custom"
             MaxFileSize="900000000000" />
        </listeners>
      </source>
    </sources>
    <trace autoflush="true">
      <listeners>
        <clear />
        <add name="defaultListener" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
             initializeData="FileLogWriter"
             Append="true"
             AutoFlush="true"
             BaseFileName="program"
             CustomLocation="D:\Log\App\Program"
             DiskSpaceExhaustedBehavior="DiscardMessages"
             Encoding="Unicode"
             IncludeHostName="false"
             LogFileCreationSchedule="Daily"
             location="Custom"
             MaxFileSize="900000000000" />
        <add name="programConsoleListener" type="System.Diagnostics.ConsoleTraceListener" />
      </listeners>
    </trace>
  </system.diagnostics>
  ...
</configuration>

并使用它定义您的 TraceSource:

static TraceSource dataSource = new TraceSource("DataSource");
static TraceSource errorSource = new TraceSource("ErrorSource");

为了更轻松地使用 TraceSource(对于某些场景),我编写了一个扩展方法:

public static void WriteLine(this TraceSource source, object o)
{
    var str = (o ?? string.Empty).ToString();

    if (source.Listeners == null || source.Listeners.Count == 0) throw new InvalidOperationException(string.Format("TraceSource named {0} has no listeners", source.Name));

    foreach (TraceListener listener in source.Listeners)
        listener.WriteLine(str);
}

这对我有用。

但您无法根据调用 TraceSource 的代码块对一个应用程序域中的 TraceSource 进行分类。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用多个跟踪侦听器 的相关文章

随机推荐