.net 如何记录 WCF 消息内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8051239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to log WCF message content?
提问by Chris F Carroll


I thought this would be simple, but I can't see how to tell WCF to log message bodies. I have:
我认为这很简单,但我不知道如何告诉 WCF 记录消息正文。我有:
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Verbose">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EntLibLoggingProxyTraceListener,Microsoft.Practices.EnterpriseLibrary.Logging"
name="traceListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
</system.diagnostics>
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true" logMalformedMessages="true"
logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="false" />
</diagnostics>
...etc..,
...etc...
</system.Model>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add fileName="_trace-xml.log"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData,Microsoft.Practices.EnterpriseLibrary.Logging"
traceOutputOptions="None"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener,Microsoft.Practices.EnterpriseLibrary.Logging"
name="XML Trace Listener" />
...etc... other listeners
</listeners>
...etc...
</loggingConfiguration>
But all I get logged is stuff about the message, not the message body. What do I need to change to log message content?
但是我记录的只是关于消息的内容,而不是消息正文。我需要更改什么才能记录消息内容?
采纳答案by JohnC
If you need message bodies, you'll have to capture them yourself, programatically. Use a service behavior to install a message inspector and capture messages inside your implementation of IDispatchMessageInspector or IClientMessageInspector.
如果您需要消息正文,则必须以编程方式自己捕获它们。使用服务行为安装消息检查器并捕获 IDispatchMessageInspector 或 IClientMessageInspector 实现中的消息。
回答by kroonwijk
Just following this description works perfectly for me: http://msdn.microsoft.com/en-us/library/ms730064.aspx
只需遵循此描述对我来说非常有效:http: //msdn.microsoft.com/en-us/library/ms730064.aspx
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="messages"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\logs\messages.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
<system.serviceModel>
<diagnostics>
<messageLogging
logEntireMessage="true"
logMalformedMessages="false"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="false"
maxMessagesToLog="3000"
maxSizeOfMessageToLog="2000"/>
</diagnostics>
</system.serviceModel>
No need to write any code for that ...
无需为此编写任何代码......
回答by RichardHowells
I found an versioning issue. When the WCF Service project was built against .Net 3.5 I saw the full message body in the viewer tool. Built against .Net 4.0 I saw no message body.
我发现了一个版本控制问题。当针对 .Net 3.5 构建 WCF 服务项目时,我在查看器工具中看到了完整的消息正文。针对 .Net 4.0 构建我没有看到消息正文。
回答by solowk
Try http://www.csharptutorial.in/12/csharp-net-wcf-how-to-enable-wcf-tracing-and-messageloggingand setup logMessagesAtTransportLevel="true". In your log file you can find <s:Body>tags. Investigate text near them. Later you can add new "XmlListener" and found the message body in more flexible format.
尝试http://www.csharptutorial.in/12/csharp-net-wcf-how-to-enable-wcf-tracing-and-messagelogging并设置 logMessagesAtTransportLevel="true"。在您的日志文件中,您可以找到<s:Body>标签。调查他们附近的文字。稍后您可以添加新的“XmlListener”并以更灵活的格式找到消息正文。
回答by baur
You can consider another way to see XML SOAP - custom MessageEncoder. The main difference from similar IDispatchMessageInspector / IClientMessageInspector is that it works on lower level, so it captures original byte content including any malformed xml.
您可以考虑另一种方式来查看 XML SOAP -自定义 MessageEncoder。与类似的 IDispatchMessageInspector / IClientMessageInspector 的主要区别在于它在较低级别上工作,因此它捕获原始字节内容,包括任何格式错误的 xml。
In order to implement tracing using this approach you need to wrap a standard textMessageEncodingwith custom message encoderas new binding elementand apply that custom binding to endpoint in your config.
为了使用这种方法实现跟踪,您需要使用自定义消息编码器包装标准textMessageEncoding作为新绑定元素,并将该自定义绑定应用到您的config 中的端点。
Also you can see as example how I did it in my project - wrappingtextMessageEncoding, logging encoder, custom binding elementand config.
您还可以看到我在我的项目中是如何做的示例 - 包装textMessageEncoding、日志编码器、自定义绑定元素和config。

