2010-11-19 103 views
4

我想将跟踪信息保存到.svclog文件中,但仅限于失败的请求。这可能吗?如果是这样,确切地说?WCF只跟踪失败的请求?

我有这就是所谓的每分钟几百次的WCF服务。在极少数情况下,客户端会在WCF内部运行的代码的边界之外发生错误500(通常是安全问题)。我想知道这些错误发生的原因以及导致它们的原因。

我也真的想使用跟踪查看器工具来检查.svclog文件。

据我所知,我有两个选择: 1)通过system.webServer \ tracing设置记录失败请求的仪器FERB跟踪。不幸的是,我真的不喜欢IE跟踪查看器的界面,也没有从跟踪日志中得到足够的信息来找出为什么我的代码之外出现错误。

2)打开下System.Diagnostics程序\迹线部分的全球跟踪。本部分会生成很棒的跟踪日志,并记录下我所需要的所有内容。但是,我无法找到一种方法来捕获失败请求的信息。本部分捕获所有请求的跟踪信息。我的跟踪记录很快就填满了!

我的错误500是间歇性和罕见的。最终,我希望始终让我的.svclog跟踪开启,但只有在发生失败请求时才会启动它。

如果可能,请咨询您的意见吗?

谢谢!

编辑:

格雷厄姆, 我已经按照你的建议,我没有看到我所期望的日志。下面是从web.config相关章节:

<system.diagnostics> 
    <trace> 
     <listeners> 
      <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics"> 
       <filter type="" /> 
      </add> 
     </listeners> 
    </trace> 

    <sources> 
     <source name="System.ServiceModel" switchValue="Error"> 
      <listeners> 
       <add name="wcfTracing" 
         type="System.Diagnostics.XmlWriterTraceListener" 
         initializeData="Traces1.svclog"/> 
       <add name="log4netTracing" 
         type="AzureWatch.Model.Service.Log4netTraceListener,AzureWatch.Model.Service"/> 
      </listeners> 
     </source> 
     <source name="System.ServiceModel.MessageLogging" switchValue="Error"> 
      <listeners> 
       <add name="wcfTracing" 
         type="System.Diagnostics.XmlWriterTraceListener" 
         initializeData="Traces2.svclog"/> 
       <!--<add name="log4netTracing" 
         type="AzureWatch.Model.Service.Log4netTraceListener,AzureWatch.Model.Service"/>--> 
      </listeners> 
     </source> 
    </sources> 
    </system.diagnostics> 

<!-- ... --> 

     <diagnostics wmiProviderEnabled="true"> 

     <messageLogging 
      logEntireMessage="true" 
      logMalformedMessages="true" 
      logMessagesAtServiceLevel="true" 
      logMessagesAtTransportLevel="true" 
      maxSizeOfMessageToLog="1000000" 
      maxMessagesToLog="-1" /> 
    </diagnostics> 

这里是WCF的客户端错误:

<Exception> 
    <Type>System.Net.Sockets.SocketException</Type> 
    <Message>An existing connection was forcibly closed by the remote host</Message> 
    <StackTrace> 
     <Frame>at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)</Frame> 
    </StackTrace> 
    </Exception> 

不幸的是可以被任意的跟踪监听器的记录NOTHING。 失败请求日志中包含的:

-GENERAL_READ_ENTITY_END 
    BytesReceived 0 
    ErrorCode 2147943395 
    ErrorCode The I/O operation has been aborted because of either a thread exit or an application request. (0x800703e3) 
    Warning 
-MODULE_SET_RESPONSE_ERROR_STATUS 
    ModuleName ManagedPipelineHandler 
    Notification 128 
    HttpStatus 400 
    HttpReason Bad Request 
    HttpSubStatus 0 
    ErrorCode 0 
    ConfigExceptionInfo 
    Notification EXECUTE_REQUEST_HANDLER 
    ErrorCode The operation completed successfully. (0x0) 
    0 msInformational 

回答

6

我试图把下面的配置我的WCF服务,并与有效和无效击中证书服务。只有具有无效凭据的请求才会导致在服务跟踪文件中出现任何内容。我的服务使用一个自定义的UserNamePasswordValidator类,并且这存在于堆栈跟踪中。重要的部分是<source>元素中的switchValue="Error"propagateActivity="false"。不知道这是否是你想要什么,但至少看起来接近...

<system.diagnostics> 
    <sources> 
    <source name="System.ServiceModel" switchValue="Error" 
      propagateActivity="false"> 
     <listeners> 
     <add type="System.Diagnostics.DefaultTraceListener" name="Default"> 
      <filter type="" /> 
     </add> 
     <add name="ServiceModelTraceListener"> 
      <filter type="" /> 
     </add> 
     </listeners> 
    </source> 
    </sources> 
    <sharedListeners> 
    <add initializeData="C:\Path-to-log-file\Web_tracelog.svclog" 
     type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
     name="ServiceModelTraceListener" 
     traceOutputOptions="DateTime, Timestamp, Callstack"> 
     <filter type="" /> 
    </add> 
    </sharedListeners> 
    <trace autoflush="true" /> 
</system.diagnostics> 
+0

我会建议使用警告级别最初直到你确定了错误。 – softveda 2010-11-19 09:36:04

+0

@ Pratik-听起来够公平 - 什么会触发警告?我可以看到抛出一个异常将意味着一个错误... – 2010-11-19 09:38:57

+1

有时警告事件可能出现在实际的错误之前,并可以给错误提供额外的见解。 – softveda 2010-11-21 22:48:02

1

或者它可能至少指定EventTypeFilter作为听者的filter

<listeners> 
    <add name="console" 
     type="System.Diagnostics.ConsoleTraceListener" > 
     <filter type="System.Diagnostics.EventTypeFilter" 
     initializeData="Error" /> 
    </add> 
    </listeners>