跟踪来自客户端的 WCF 调用 [英] Trace WCF call from client

查看:24
本文介绍了跟踪来自客户端的 WCF 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个进行 WCF 调用的 Windows 窗体客户端应用程序.我希望能够从应用程序中显示实际的请求和响应,包括 SOAP 标头和有关 POST/Get 的信息.有没有办法在客户端配置跟踪监听器并从客户端使用它,在文本框中显示内容?

I've built a windows forms client application which makes a WCF call. I'd like to be able to display the actual request and response, includeing SOAP header, and information about the POST/Get, from within the application. Is there a way to configure a trace listener on the client and consume it from within the client, displaying the contents in a text box?

我已经配置了将消息输出到文件的跟踪.此配置在我的客户端应用程序上,因此它会记录对 wcf 服务和响应进行的所有调用.因此已经添加了源,并且每个源都有一个 XmlTraceListener,它处理写入 xml 日志文件.我现在要做的是利用客户端应用程序本身中的跟踪侦听器并写入文本框控件.

I've configured tracing which outputs messages to a file. This configuration is on my client application, so it's logging all calls it is making to the wcf service and the response. So the sources have been added and for each source is an XmlTraceListener, which handles writing to an xml log file. What I'm looking to do now is utilize a trace listener from within the client application itself and write to a textbox control.

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

<!-- child of the <system.serviceModel> element -->
<diagnostics>
    <messageLogging maxMessagesToLog="10000"
                    logEntireMessage="true"
                    logMessagesAtServiceLevel="true"
                    logMalformedMessages="true"
                    logMessagesAtTransportLevel="true">
        <filters>
           <clear/>
        </filters>
    </messageLogging>
</diagnostics>

所以现在我已经有了消息记录功能,我创建了自己的跟踪侦听器,可以写入文本框:

So now that I've got message logging working, I create my own trace listener that can write to a textbox:

public class MyTraceListender : System.Diagnostics.TraceListener
{
    private TextBox txt_m;
    public MyTraceListender(TextBox txt)
        : base()
    {
        txt_m = txt;
    }

    private delegate void delWrite(string sText);

    public override void Write(string message)
    {
        txt_m.Invoke(new delWrite(AsyncWrite), message);
    }

    public override void WriteLine(string message)
    {
        this.Write(message + System.Environment.NewLine);
    }

    private void AsyncWrite(string sMessage)
    {
        this.txt_m.AppendText(sMessage);
    }
}

现在我已经有了跟踪侦听器,我想尝试从我的 Windows 窗体客户端应用程序中使用它.

Now that I've got my trace listener, I want to try using it from my windows forms client application.

public partial class Form1 : Form
{
    private MyTraceListender listener_m;

    public Form1()
    {
        InitializeComponent();

        listener_m = new MyTraceListender(this.txtOutput);

        System.Diagnostics.Trace.Listeners.Add(listener_m);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
        string sValue = client.GetData(1234);
    }
}

此时,我仍然没有看到记录到文本框控件.我认为侦听器与源无关,因此我尝试了以下操作:

At this point, I'm still not seeing logging to the textbox control. I'm thinking that the listener is not associated to the source, so I tried the following:

public partial class Form1 : Form
{
    private MyTraceListender listener_m;

    public static System.Diagnostics.TraceSource source = new System.Diagnostics.TraceSource("System.ServiceModel.MessageLogging", System.Diagnostics.SourceLevels.All);

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
        string sValue = client.GetData(1234);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        listener_m = new MyTraceListender("test", this.txtOutput);
        source.Listeners.Add(listener_m);
    }
}

在所有这些之后,消息不会被记录到表单上的文本框.它们通过 XmlTraceListener 被记录到文件中,所以我假设问题是我如何通过 System.Diagnostics.Trace.Listeners.Add() 添加我的自定义侦听器.这是正确的方法吗?

After all of this, messages are not being logged to the textbox on the form. They are being logged to the file via the XmlTraceListener, so I'm assuming the issue is how I'm adding my custom listener via System.Diagnostics.Trace.Listeners.Add(). Is this the correct approach?

推荐答案

这个解决方案主要是通过 app.config.如果您更喜欢基于代码的解决方案,请告诉我.

This solution is mostly through app.config. Let me know if you prefer a code-based solution.

在 app.config 中,将您的侦听器添加到共享侦听器列表中,以及消息日志记录源的侦听器.在 'listeners' 列表中,名称必须与稍后在 'sharedListeners' 列表中的名称相匹配.在 'sharedListeners' 列表中,'type' 必须包括完整的类名和命名空间,以及程序集名称:

In app.config, add your listener to the list of shared listeners, as well as a listener for the message logging source. In 'listeners' list, the name must match the name later on in the 'sharedListeners' list. In 'sharedListeners' list, the 'type' must include full class name with namespace, as well as assembly name:

<system.diagnostics>
<sources>
  <source name="System.ServiceModel" switchValue="All">
    <listeners>
      <add name="xmlTraceListener"/>
    </listeners>
  </source>
  <source name="System.ServiceModel.MessageLogging" switchValue="All">
    <listeners>
      <add name="xmlTraceListener"/>
      <add name="textBoxListener"/>
    </listeners>
  </source>
</sources>
<sharedListeners>
  <add name="xmlTraceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="ClientLogBasic.svclog"/>
  <add name="textBoxListener" type="WinTransmitterClient.MyTraceListener, WinTransmitterClient" initializeData=""/>
</sharedListeners>
<trace autoflush="true"/>

由于 MyTraceListener 将由框架构造,因此其构造函数必须与基类匹配.因此,应将文本框设为可在需要时设置的属性.

Because MyTraceListener will be constructed by the framework, its constructor must match the base class. So instead, make the textbox a property that can be set when needed.

在 MyTraceListener.cs 中:

In MyTraceListener.cs:

public class MyTraceListener : System.Diagnostics.TraceListener
{
    public TextBox txt_m
    {
        get;set;
    }          

    public MyTraceListener()
        : base()
    {}

    // rest as before ...

在Form1.cs中,在创建客户端后抓取自定义监听器,并设置文本框.不需要其他代码.这是整个 Form1.cs,不包括 'using' 和 'namespace' 行:

In Form1.cs, grab the custom listener after the client is created, and set the text box. No other code is needed. This is the entire Form1.cs, excluding 'using' and 'namespace' lines:

public partial class Form1 : Form
{
    public static System.Diagnostics.TraceSource source = new System.Diagnostics.TraceSource("System.ServiceModel.MessageLogging", System.Diagnostics.SourceLevels.All);

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ServiceReference1.ContactManagerTextServiceClient client = new ServiceReference1.ContactManagerTextServiceClient();

        // identifier in quotes must match name from config file
        MyTraceListener mtl = source.Listeners["textBoxListener"] as MyTraceListener;

        // of course this doesn't need to be done at every button click, but you get the idea
        mtl.txt_m = this.txtOutput;

        string sValue = client.GetData(1234);

    }
}

这篇关于跟踪来自客户端的 WCF 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆