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

khl*_*hlr 5 wcf logging xpath message filter

我有一个WCF服务与以下合同:

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

    [OperationContract]
    string ToUpper(string s);
}
Run Code Online (Sandbox Code Playgroud)

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

在这里,我将介绍如何过滤写入日志的消息.但我必须做错事,因为我的日志仍然是空的......我的配置看起来像这样

<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>
Run Code Online (Sandbox Code Playgroud)

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

没有过滤器,默认消息的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>
Run Code Online (Sandbox Code Playgroud)

更新: 对于每个对解决方案感兴趣的机构,我终于得到了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>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

这样一来,不仅关系到信息Invert,ToUpper并且Method2被记录.

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

jas*_*sso 7

Action在XPath表达式中使用了错误的命名空间

你有

xmlns:a="http://www.w3.org/2005/08/addressing"
... /a:Action[starts-with ...
Run Code Online (Sandbox Code Playgroud)

而且文件有

<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">
Run Code Online (Sandbox Code Playgroud)

因此命名空间不同,因为Actionelement附加了一个默认的命名空间定义.

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


小智 5

非常感谢您提供有用的信息!

根据我自己的研究,要成功执行过滤,还必须执行以下关键点:

  1. 不要以双“/”开头 XPath 表达式,否则过滤将根本不起作用。尽管这个双“/”对于 XPath 语法来说是正确的(与 jasso 于 2011 年 5 月 27 日 23:06 指出的相同)。
  2. 不要依赖 WCF 配置编辑工具来选择所涉及的 xml 命名空间的别名。到目前为止发现“s12”不起作用,但“s”或“s1a”可以(也许这是微软的一个错误,我不确定)。