在反序列化到我的数据契约(Contract)类之前,如何记录发送到我的 WCF REST 服务的 xml?
最佳答案
您可以使用 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: Configuring Tracing 中阅读有关 WCF 跟踪的更多信息.
Microsoft 提供了一个 Service Trace Viewer Tool读取 .svclog 文件。
确保 initializeData
中定义的路径可由您的服务写入。
关于WCF REST 调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2135238/