0

我使用以下代码实现了 WCF 跟踪:

    <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="All"
        propagateActivity="true">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="xml">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source name="CardSpace" switchValue="All">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="xml">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source name="System.IO.Log" switchValue="All">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="xml">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source name="System.Runtime.Serialization" switchValue="All">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="xml">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source name="System.IdentityModel" switchValue="All">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="xml">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging" switchValue="All">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelMessageLoggingListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="c:\wcflogs\trace.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="Default" traceOutputOptions="Timestamp">
        <filter type="" />
      </add>
    </sharedListeners>
    <trace autoflush="true" />
  </system.diagnostics>

  <system.serviceModel>
    <diagnostics wmiProviderEnabled="true">
      <messageLogging logEntireMessage="true" logMalformedMessages="true"
        logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
        maxMessagesToLog="500" maxSizeOfMessageToLog="5000" />
    </diagnostics>
  </system.serviceModel>

但它只跟踪在 try/catch 块之外抛出的异常,所以当我想在我的跟踪中有这个异常时,我需要删除 try/catch 块。有没有办法在我的 WCF 跟踪中也有 try {} 的这个异常?

4

1 回答 1

0

只需将另一个源添加到您的配置文件中。您应该仍然可以使用共享侦听器。我认为。

<system.diagnostics>
  <sources>
   <source name="HandledExceptions" switchValue="All">
    <listeners>
     <add initializeData="HandledExceptions.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="HandledExceptionListener">
      <filter type="" />
     </add>
    </listeners>
   </source>
  </sources>
  <trace autoflush="true" />
 </system.diagnostics>

然后在代码中写一个事件

catch (FaultException fe)
{
    TraceSource trace = new TraceSource("HandledExceptions");
    trace.TraceEvent(TraceEventType.Warning, 0, fe.Message + System.Environment.NewLine + fe.StackTrace);
    trace.Flush();
    trace.Close();

    throw fe;
}
catch (Exception e)
{
    TraceSource trace = new TraceSource("HandledExceptions");
    trace.TraceEvent(TraceEventType.Critical, 0, e.Message + System.Environment.NewLine + e.StackTrace);
    trace.Flush();
    trace.Close();

    throw new FaultException(e.Message);
}
于 2015-07-01T04:49:57.743 回答