WCF REST调试

3

在我的WCF REST服务中,如何记录发送到服务的xml内容,在反序列化为数据合同类之前?

2个回答

4
你可以使用WCF跟踪来记录原始的XML消息。
默认情况下,跟踪是禁用的。你可以通过编辑应用程序的配置文件来启用和配置跟踪。以下的 .config 示例启用了带有原始消息记录的WCF跟踪:
<configuration>
  <system.serviceModel>
    <diagnostics>
      <messageLogging maxMessagesToLog="30000"
              logEntireMessage="true"
              logMessagesAtServiceLevel="true"
              logMalformedMessages="true"
              logMessagesAtTransportLevel="true">
      </messageLogging>
    </diagnostics>
  </system.serviceModel>
  <system.diagnostics>
    <sources>
      <source name="System.IdentityModel" 
              switchValue="Verbose" 
              logKnownPii="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <!-- Log all messages in the 'Messages' tab of SvcTraceViewer. -->
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <!-- ActivityTracing and propogateActivity are used to 
           flesh out the 'Activities' tab in SvcTraceViewer to 
           aid debugging. -->
      <source name="System.ServiceModel" 
              switchValue="Error, ActivityTracing" 
              propagateActivity="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <!-- This records Microsoft.IdentityModel generated traces, 
           including exceptions thrown from the framework. -->
      <source name="Microsoft.IdentityModel" switchValue="Warning">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml" 
           type="System.Diagnostics.XmlWriterTraceListener" 
           initializeData="C:\logs\trace.svclog" />
    </sharedListeners>
    <trace autoflush="true" />
  </system.diagnostics>
</configuration>

您可以从MSDN:配置跟踪了解更多关于WCF跟踪的信息。
微软提供服务跟踪查看器工具来读取.svclog文件。
请确保initializeData中定义的路径可被您的服务写入。

1
服务跟踪查看器非常棒。 - Jay

1

如果您想查看原始的HTTP流量,像Fiddler这样的代理工具是最简单的方法。您将能够看到所有已POST/PUT到REST服务的信息。

如果您的意思是“日志”就是“始终将HTTP流量写入文件的特定位置”,那么您可以使用内置跟踪来完成大部分工作。这里有一个链接提供了一个示例,否则只需在网上搜索“WCF跟踪”,您将找到大量优秀的示例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接