WCF消息日志记录 - 使用XPath查询添加过滤器

时间:2011-05-27 18:06:16

标签: wcf logging xpath filter message

我有一个WCF服务,其中包含以下合同:

[ServiceContract(Namespace="http://myNamespace.org/")]
public interface IMyService
{
    [OperationContract]
    string Invert(string s);

    [OperationContract]
    string ToUpper(string s);
}

客户调用两种方法InvertToUpper。想象一下,我想使用消息记录,但我感兴趣的唯一方法是ToUpper,因为另一种方法被大量使用并且记录所有消息会破坏日志;)

Here,我读了如何过滤写入日志的消息。但我必须做错事,因为我的日志仍然是空的......我的配置看起来像这样

<system.serviceModel>
  ...
  <diagnostics>
    <messageLogging logEntireMessage="true" logMessagesAtServiceLevel="false" logMalformedMessages="true" logMessagesAtTransportLevel="true">
      <filters>
        <add xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">/soap:Envelope/soap:Header/a:Action[starts-with(text(),'http://myNamespace.org/IMyService/ToUpper')]</add>
      </filters>
    </messageLogging>
  </diagnostics>

</system.serviceModel>

<system.diagnostics>
  <sources>
    <source name="System.ServiceModel.MessageLogging">
      <listeners>
        <add name="ServiceModelTraceListener" />
      </listeners>
    </source>
  </sources>
  <sharedListeners>
    <add initializeData="LogServer.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="ServiceModelTraceListener" />
  </sharedListeners>
  <trace autoflush="true" />
</system.diagnostics>

如果我应用此过滤器,则不会在日志中输入任何消息... 那么关于上面的链接示例我做错了什么?

没有过滤器,默认消息的xml跟踪(使用字符串参数ToUpper调用的方法hello)如下所示:

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
  <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
    <EventID>0</EventID>
    <Type>3</Type>
    <SubType Name="Information">0</SubType>
    <Level>8</Level>
    <TimeCreated SystemTime="2011-05-27T17:53:53.9908714Z" />
    <Source Name="System.ServiceModel.MessageLogging" />
    <Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" />
    <Execution ProcessName="WcfLoggingTest.Host.vshost" ProcessID="4324" ThreadID="12" />
    <Channel />
    <Computer>MY-Machine</Computer>
  </System>
  <ApplicationData>
    <TraceData>
      <DataItem>
        <MessageLogTraceRecord Time="2011-05-27T19:53:53.9908714+02:00" Source="TransportReceive" Type="System.ServiceModel.Channels.BufferedMessage" xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
          <HttpRequest>
            <Method>POST</Method>
            <QueryString></QueryString>
            <WebHeaders>
              <VsDebuggerCausalityData>uIDPozEtlPQCjkhCodYdPWh6joUAAAAAamILDP7v3kG5sY6zKsB7HPPiLBWr+AVGmfFDQbk8GYAACQAA</VsDebuggerCausalityData>
              <SOAPAction>"http://myNamespace.org/IMyService/ToUpper"</SOAPAction>
              <Content-Length>157</Content-Length>
              <Content-Type>text/xml; charset=utf-8</Content-Type>
              <Accept-Encoding>gzip, deflate</Accept-Encoding>
              <Expect>100-continue</Expect>
              <Host>localhost:8731</Host>
            </WebHeaders>
          </HttpRequest>
          <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
            <s:Header>
              <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:8731/Design_Time_Addresses/MyService/</To>
              <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://myNamespace.org/IMyService/ToUpper</Action>
            </s:Header>
            <s:Body>
              <ToUpper xmlns="http://myNamespace.org/">
                <s>hello</s>
              </ToUpper>
            </s:Body>
          </s:Envelope>
        </MessageLogTraceRecord>
      </DataItem>
    </TraceData>
  </ApplicationData>
</E2ETraceEvent>

更新 对于每个对解决方案感兴趣的机构,我终于得到了jasso的帮助,谢谢:

<add xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">/soap:Envelope/soap:Header/a:Action[starts-with(text(),'http://myNamespace.org/IMyService/ToUpper')]</add>

然后我编辑了界面,并将方法Method1添加到Method3。我的目标是记录除Method1Method3相关消息之外的所有内容。我用以下过滤器做了这个:

<add xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">/soap:Envelope/soap:Header/a:Action[starts-with(text(),'http://myNamespace.org/IMyService/Method1')=false() and starts-with(text(),'http://myNamespace.org/IMyService/Method3')=false()]</add>

这样,只会记录与InvertToUpperMethod2相关的消息。

使用两个单独的过滤器处理这个问题可能更简洁,但目前我对此非常满意。

3 个答案:

答案 0 :(得分:7)

您对XPath表达式中的Action元素使用了错误的命名空间

你有

xmlns:a="http://www.w3.org/2005/08/addressing"
... /a:Action[starts-with ...

并且文档已

<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">

因此命名空间不同,因为Action元素附加了默认的命名空间定义。

此外,您的XPath正在搜索soap:Envelope root 元素,因为您的表达式以/开头。我不熟悉该框架,它可能会从您的示例XML(soap内容)中选择一个子树,然后应用XPath过滤器。如果不是这种情况并且您的XPath应该在给定的XML文档上产生匹配,那么您应该使用//soap:Envelope元素的路径(如/*/*/*/*/*/soap:Envelope)启动表达式。在开头使用//运算符是低效的,因为它需要遍历整个文档中的所有节点。

答案 1 :(得分:4)

非常感谢有用的信息!

根据我自己的研究,要成功执行过滤,还必须强制执行这些要点:

  1. 不要使用双“/”启动XPath表达式,否则过滤根本不起作用。虽然这个双“/”在XPath语法方面是正确的(与2011年5月27日23:06 jasso指出的相同)。
  2. 不依赖于WCF配置编辑工具来选择所涉及的xml命名空间的别名。直到现在发现“s12”不起作用,但“s”或“s1a”没问题(也许这是微软完成的一个bug,我不确定)。

答案 2 :(得分:0)

XPath查询可能就是问题所在。试试这个更简单的版本:

<filters>
    <add xmlns:msgtr="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace" >//msgtr:SOAPAction[contains(.,'ToUpper')]</add>
</filters>