c# - WCF REST 消息记录、POST 请求问题

标签 c# wcf rest post logging

我获得了 WCF REST 服务,我希望记录从此服务收到/发送的每条消息。在我的 app.config 中,我得到了这个配置:

 <system.serviceModel>
  <diagnostics>
      <messageLogging logEntireMessage="true" logMalformedMessages="true"
        logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" />
    </diagnostics>
  </system.serviceModel>    
<system.diagnostics>
    <sources>
    <source name="System.ServiceModel.MessageLogging" switchValue="Warning,ActivityTracing">
            <listeners>
                <add type="System.Diagnostics.DefaultTraceListener" name="Default">
                    <filter type="" />
                </add>
                <add name="ServiceModelMessageLoggingListener">
                    <filter type="" />
                </add>
            </listeners>
        </source>     
    </sources>
    <sharedListeners>
      <add initializeData="logs\\messages.svclog"
              type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
              name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
              <filter type="" />
          </add>
    </sharedListeners>
    <trace autoflush="true" />
  </system.diagnostics>

我正在为我的服务使用这个绑定(bind)

 WebHttpBinding basicWebBinding = new WebHttpBinding
        {
            MaxReceivedMessageSize = int.MaxValue,
            MaxBufferSize = int.MaxValue,
            MaxBufferPoolSize = int.MaxValue,
            CloseTimeout = new TimeSpan(0, 3, 0),
            OpenTimeout = new TimeSpan(0, 3, 0),
            ReceiveTimeout = new TimeSpan(0, 3, 0),
            TransferMode = TransferMode.Buffered,
            SendTimeout = new TimeSpan(0, 3, 0),
            WriteEncoding = Encoding.UTF8,
            ReaderQuotas = new XmlDictionaryReaderQuotas()
            {
                MaxStringContentLength = int.MaxValue,     
            }
        };

还有这个操作契约(是的,它模拟了aspx页面)

 [OperationContract(Name = "process.aspx")]
        [WebInvoke(Method = "POST",ResponseFormat = WebMessageFormat.Xml)]
          Stream ProcessRequest(Stream data);

Everythig 似乎很好,但我看不到请求的任何 POST 数据,例如

<MessageLogTraceRecord>
<HttpRequest xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
<Method>POST</Method>
<QueryString></QueryString>
<WebHeaders>
<Connection>keep-alive</Connection>
<Content-Length>123</Content-Length>
<Content-Type>application/x-www-form-urlencoded</Content-Type>
<Accept>text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8</Accept>
<Accept-Encoding>gzip, deflate</Accept-Encoding>
<Accept-Language>ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3</Accept-Language>
<Host>localhost</Host>
<Referer>http://localhost/...</Referer>
<User-Agent>Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0</User-Agent>
</WebHeaders>
</HttpRequest>
<Binary xmlns=""></Binary>
</MessageLogTraceRecord>

和响应

<MessageLogTraceRecord>
<HttpRequest xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
<Method>POST</Method>
<QueryString></QueryString>
<WebHeaders>
<Connection>keep-alive</Connection>
<Content-Length>159</Content-Length>
<Content-Type>application/x-www-form-urlencoded</Content-Type>
<Accept>*/*</Accept>
<Accept-Encoding>gzip, deflate</Accept-Encoding>
<Accept-Language>ru</Accept-Language>
<Host>localhost...</Host>
<User-Agent>agent</User-Agent>
</WebHeaders>
</HttpRequest>... stream ...</MessageLogTraceRecord>

任何帮助都会很好。

最佳答案

我的猜测是 POST 数据超过了默认的消息记录长度。

您是否尝试过将 maxSizeOfMessageToLog="200000"添加到 messageLogging 元素? (当然你可以根据需要设置大小)

这是我成功使用的设置。

<messageLogging 
logEntireMessage="true" 
logMalformedMessages="false"
logMessagesAtServiceLevel="true" 
logMessagesAtTransportLevel="false"
maxMessagesToLog="3000"
maxSizeOfMessageToLog="200000"/>

干杯 克里斯

关于c# - WCF REST 消息记录、POST 请求问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13494036/

相关文章:

c# - 带有 'same port' 的 WCF 多主机 header 导致错误

java - Spring MVC : ResponseBodyAdvice doesn't get called for null response from controller

c# - 您如何访问委托(delegate)目标方法参数?

c# - 构建队列服务 - 序列化任务供以后使用

c# - 如何退出所有正在运行的线程?

WCF REST 服务在不活动后进入休眠状态

json - 在一个盒子里休息后端?

java - 如何在curl中调用具有两个请求正文参数的endPoint

c# - 清除 ASP.NET 中的文本框历史记录

c# - 在自定义 UserControl 上强制使用透明背景