捕获尝试捕获wcf跟踪中的异常

时间:2013-07-29 14:26:36

标签: asp.net wcf exception trace

我使用以下代码实现了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块之外的exeptions,因此当我想在跟踪中有这个异常时,我需要删除try / catch块。有没有办法在我的WCF跟踪中从try {}获得此异常?

1 个答案:

答案 0 :(得分:0)

只需在配置文件中添加其他来源即可。您仍然可以使用共享侦听器。我想。

id

然后在代码中编写一个事件

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