使用WCF的Trace与Message Log功能

简介:

前面介绍过如何创建一个WCF Service http://www.cnblogs.com/jfzhu/p/4025448.html,本文介绍一下如何开启与查看WCF的Message Log和Trace Log。

 

(一)使用WCF Service Configuration Editor开启Trace与Message Log功能

如果安装了Visual Studio,我们可以使用WCF Service Configuration Editor来开启WCF的Trace和Message Log的功能。Trace和Log既可以在客户端做,也可以在服务器端做。

 

有两种方式可以打开WCF Service Configuration Editor

(1)Visual Studio Tools –> WCF Service Configuration Editor:

image

 

(2)直接在Solution Explorer中右键点击app.config –> Edit WCF Configuration:

image

 

打开WCF Service Configuration Editor后,我们需要点击

  • Enable Log Auto-Flush
  • Enable MessageLogging
  • Enable Tracing

image

image

 

还要将LogEntireMessage改为True:

image

 

保存配置文件,可以看到文件夹中多了两个文件,一个是message log文件,另一个是trace log文件:

 

image

 

(二)调用WCF Service

演示的解决方案如下:

image

 

Employee.cs:

复制代码
namespace HelloService
{
    [DataContract(Namespace="http://jfzhu.com/2014/10/16/Employee")]
    public class Employee
    {
        private int id;
        private string name;
        private DateTime birthdate;

        [DataMember(Name="Id", Order=1)]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        [DataMember(Order = 2)]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        [DataMember(Order = 3)]
        public DateTime Birthdate
        {
            get { return birthdate; }
            set { birthdate = value; }
        }
    }
}
复制代码

 

IHelloService.cs

复制代码
namespace HelloService
{    
    [ServiceContract(Name = "IHelloService")]
    public interface IHelloService
    {
        [OperationContract(Name="GetMessage")]
        string GetMessage(string name);

        [OperationContract]
        Employee GetEmployee(int id);
    }
}
复制代码

 

HelloService.cs

复制代码
namespace HelloService
{    
    public class HelloService : IHelloService
    {
        public string GetMessage(string name)
        {
            return "Hello " + name;
        }

        public Employee GetEmployee(int id)
        {
            return new Employee() { Id = id, Name="Neil Klugman", Birthdate=new DateTime(1930, 1, 31)};
        }
    }
}
复制代码

 

Host的web.config

复制代码
<?xml version="1.0" encoding="utf-8"?>
<configuration>  
  <system.serviceModel>
    <diagnostics>
      <messageLogging logEntireMessage="true" logMalformedMessages="true"
        logMessagesAtTransportLevel="true" />
    </diagnostics>
    <services>
      <service name="HelloService.HelloService" behaviorConfiguration="metaBehavior">
        <endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metaBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
复制代码


Client的Form1.cs

复制代码
namespace WindowsClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonGetMessage_Click(object sender, EventArgs e)
        {
            HelloService.HelloServiceClient client = new HelloService.HelloServiceClient();
            labelGetMessage.Text = client.GetMessage(textBoxGetMessage.Text);
        }

        private void buttonGetEmployee_Click(object sender, EventArgs e)
        {
            HelloService.HelloServiceClient client = new HelloService.HelloServiceClient();
            HelloService.Employee employee = client.GetEmployee(Convert.ToInt32(textBoxGetEmployee.Text));
            labelId.Text = "Id: " + employee.Id.ToString();
            labelName.Text = "Name: " + employee.Name;
            labelBirthdate.Text = "Birthdate: " + employee.Birthdate.ToString("yyyy-MM-dd");
        }
    }
}
复制代码

 

Client的app.config

复制代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <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>
            <source propagateActivity="true" name="System.ServiceModel" switchValue="Warning,ActivityTracing">
                <listeners>
                    <add type="System.Diagnostics.DefaultTraceListener" name="Default">
                        <filter type="" />
                    </add>
                    <add name="ServiceModelTraceListener">
                        <filter type="" />
                    </add>
                </listeners>
            </source>
        </sources>
        <sharedListeners>
            <add initializeData="c:\temp\helloservice\windowsclient\app_messages.svclog"
                type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
                <filter type="" />
            </add>
            <add initializeData="c:\temp\helloservice\windowsclient\app_tracelog.svclog"
                type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
                <filter type="" />
            </add>
        </sharedListeners>
        <trace autoflush="true" />
    </system.diagnostics>
    <system.serviceModel>
        <diagnostics>
            <messageLogging logEntireMessage="true" logMalformedMessages="true"
                logMessagesAtTransportLevel="true" />
        </diagnostics>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IHelloService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://192.168.6.47:8080/HelloService.svc/HelloService"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IHelloService"
                contract="HelloService.IHelloService" name="BasicHttpBinding_IHelloService" />
        </client>
    </system.serviceModel>
</configuration>
复制代码

 

可以看到整个<system.diagnostics>部分都是用来配置Message Log和Trace Log的。

运行客户端,分别点击GetMessage按钮和GetEmployee按钮,调用两个WCF Service。

 

image

 

(三)使用Microsoft Service Trace Viewer查看Message Log

使用Microsoft Service Trace Viewer来看Message Log文件app_messages.svclog(除了直接双击文件可以打开查看之外,也可以在VS Command Prompt上键入命令 SVCTRACEVIWER)。

因为调用了两次WCF Service,所以在左边面板看到两个Activity。

右边面板上面的第一条记录是Request,第二条记录是Reponse。

 

GetMessage的request

 

image

 

复制代码
<MessageLogTraceRecord>
  <Addressing xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
    <Action>http://tempuri.org/IHelloService/GetMessage</Action>
    <To>http://192.168.6.47:8080/HelloService.svc/HelloService</To>
  </Addressing>
  <HttpRequest xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
    <Method>POST</Method>
    <QueryString></QueryString>
    <WebHeaders>
      <VsDebuggerCausalityData>uIDPo0e70YqoeAJIhBNYwyqk3TgAAAAAdUnEReYNIkKGPM4fV5qHKsd96tBerBNIg+gFdw79jjIACQAA</VsDebuggerCausalityData>
    </WebHeaders>
  </HttpRequest>
  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
      <ActivityId CorrelationId="f8f62c36-528f-40c7-9b4b-b6b45b57bedd" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">271f2185-f90c-43b2-9086-ba9b8e0b3bd8</ActivityId>
    </s:Header>
    <s:Body>
      <GetMessage xmlns="http://tempuri.org/">
        <name>JF</name>
      </GetMessage>
    </s:Body>
  </s:Envelope>
</MessageLogTraceRecord>
复制代码

 

GetMessage的response

 

image

复制代码
<MessageLogTraceRecord>
  <HttpResponse xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
    <StatusCode>OK</StatusCode>
    <StatusDescription>OK</StatusDescription>
    <WebHeaders>
      <Content-Encoding></Content-Encoding>
      <Vary>Accept-Encoding</Vary>
      <Content-Length>145</Content-Length>
      <Content-Type>text/xml; charset=utf-8</Content-Type>
      <Date>Thu, 16 Oct 2014 14:37:35 GMT</Date>
      <Server>Microsoft-IIS/8.0</Server>
      <X-Powered-By>ASP.NET</X-Powered-By>
    </WebHeaders>
  </HttpResponse>
  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header></s:Header>
    <s:Body>
      <GetMessageResponse xmlns="http://tempuri.org/">
        <GetMessageResult>Hello JF</GetMessageResult>
      </GetMessageResponse>
    </s:Body>
  </s:Envelope>
</MessageLogTraceRecord>
复制代码

 

GetEmployee的request

 

image

 

复制代码
<MessageLogTraceRecord>
  <Addressing xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
    <Action>http://tempuri.org/IHelloService/GetEmployee</Action>
    <To>http://192.168.6.47:8080/HelloService.svc/HelloService</To>
  </Addressing>
  <HttpRequest xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
    <Method>POST</Method>
    <QueryString></QueryString>
    <WebHeaders>
      <VsDebuggerCausalityData>uIDPo0i70YqoeAJIhBNYwyqk3TgAAAAAdUnEReYNIkKGPM4fV5qHKsd96tBerBNIg+gFdw79jjIACQAA</VsDebuggerCausalityData>
    </WebHeaders>
  </HttpRequest>
  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
      <ActivityId CorrelationId="478b437e-923a-41bc-bbd0-fc932fa664ed" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">0fa3ee85-87fc-4a76-8047-965b049882c6</ActivityId>
    </s:Header>
    <s:Body>
      <GetEmployee xmlns="http://tempuri.org/">
        <id>1</id>
      </GetEmployee>
    </s:Body>
  </s:Envelope>
</MessageLogTraceRecord>
复制代码

 

GetEmployee的response

 

image

 

复制代码
<MessageLogTraceRecord>
  <HttpResponse xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
    <StatusCode>OK</StatusCode>
    <StatusDescription>OK</StatusDescription>
    <WebHeaders>
      <Content-Encoding></Content-Encoding>
      <Vary>Accept-Encoding</Vary>
      <Content-Length>247</Content-Length>
      <Content-Type>text/xml; charset=utf-8</Content-Type>
      <Date>Thu, 16 Oct 2014 14:37:38 GMT</Date>
      <Server>Microsoft-IIS/8.0</Server>
      <X-Powered-By>ASP.NET</X-Powered-By>
    </WebHeaders>
  </HttpResponse>
  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header></s:Header>
    <s:Body>
      <GetEmployeeResponse xmlns="http://tempuri.org/">
        <GetEmployeeResult xmlns:a="http://jfzhu.com/2014/10/16/Employee" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
          <a:Id>1</a:Id>
          <a:Name>Neil Klugman</a:Name>
          <a:Birthdate>1930-01-31T00:00:00</a:Birthdate>
        </GetEmployeeResult>
      </GetEmployeeResponse>
    </s:Body>
  </s:Envelope>
</MessageLogTraceRecord>
复制代码

 

(四)使用Fiddler来查看Message Log

如果使用Fiddler来查看Message Log的话,就不需要使用WCF Service Configuration Editor开启Log功能了。下图Fiddler中左边面板的两条记录分表表示两次WCF Service的调用。

右边面板上半部分表示request,下半部分表示response,基本和Microsoft Service Trace Viewer的布局差不多。

 

image

 

GetMessage的调用:

 

image

 

image

 

request:

复制代码
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <ActivityId CorrelationId="a0148182-42f9-421c-b7d0-7a9f3edb7364" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">7b920c1f-51b6-4ee0-9e32-e2f01fcd2e4c</ActivityId>
  </s:Header>
  <s:Body>
    <GetMessage xmlns="http://tempuri.org/">
      <name>JF</name>
    </GetMessage>
  </s:Body>
</s:Envelope>
复制代码

 

response:

复制代码
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <GetMessageResponse xmlns="http://tempuri.org/">
      <GetMessageResult>Hello JF</GetMessageResult>
    </GetMessageResponse>
  </s:Body>
</s:Envelope>
复制代码

 

GetEmployee的调用:

 

image

 

image

 

request:

复制代码
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <ActivityId CorrelationId="ee409ce5-5ac4-4526-9f24-155bdfe86b0e" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">2968a624-c51a-4247-a4ce-73e0548585fd</ActivityId>
  </s:Header>
  <s:Body>
    <GetEmployee xmlns="http://tempuri.org/">
      <id>1</id>
    </GetEmployee>
  </s:Body>
</s:Envelope>
复制代码

 

 

response:

复制代码
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <GetEmployeeResponse xmlns="http://tempuri.org/">
      <GetEmployeeResult xmlns:a="http://jfzhu.com/2014/10/16/Employee" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:Id>1</a:Id>
        <a:Name>Neil Klugman</a:Name>
        <a:Birthdate>1930-01-31T00:00:00</a:Birthdate>
      </GetEmployeeResult>
    </GetEmployeeResponse>
  </s:Body>
</s:Envelope>
复制代码

 

 

(五)总结

  • Visual Studio提供了自己的Message Log和Trace Log的功能,本文主要演示了如何开启他们,以及如何查看Message Log。
  • 开启这些功能可以使用WCF Service Configuration Editor。
  • 查看这些Log可以使用Microsoft Service Trace Viewer。
  • Logging功能可以在客户端做也可以在服务器端做。
  • 不使用微软提供的工具,使用Fiddler也可以监控到调用WCF Service时发送和收到的消息。










本文转自JF Zhu博客园博客,原文链接:http://www.cnblogs.com/jfzhu/p/4030008.html    ,如需转载请自行联系原作者




相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
3月前
|
存储 Go
Go 浅析主流日志库:从设计层学习如何集成日志轮转与切割功能
本文将探讨几个热门的 go 日志库如 logrus、zap 和官网的 slog,我将分析这些库的的关键设计元素,探讨它们是如何支持日志轮转与切割功能的配置。
103 0
Go 浅析主流日志库:从设计层学习如何集成日志轮转与切割功能
|
6月前
|
C++
C++-实现日志log功能
C++-实现日志log功能
|
6月前
|
存储 运维 监控
如何在 Spring Boot 中设计和实现业务操作日志功能?
如何在 Spring Boot 中设计和实现业务操作日志功能?
1005 4
|
3月前
|
数据可视化 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用相机日志跟踪功能(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用相机日志跟踪功能(C++)
33 0
|
9月前
|
SQL XML 前端开发
Spring Boot + vue-element 开发个人博客项目实战教程(十八、操作日志功能实现)2
Spring Boot + vue-element 开发个人博客项目实战教程(十八、操作日志功能实现)2
91 0
Spring Boot + vue-element 开发个人博客项目实战教程(十八、操作日志功能实现)2
|
4月前
|
设计模式 Java 数据库连接
设计模式与面向对象编程:举例说明在Java中应用工厂模式的场景,并编写一个简单的工厂模式实现。编写一个Java装饰器,用于添加日志记录功能到现有方法上。
设计模式与面向对象编程:举例说明在Java中应用工厂模式的场景,并编写一个简单的工厂模式实现。编写一个Java装饰器,用于添加日志记录功能到现有方法上。
23 0
|
5月前
|
JSON 监控 数据可视化
实时日志分析:通过Golang编写实时日志分析模块,加强公司监控管理软件的日志监控功能
在当今数字化时代,企业面临着庞大而复杂的网络环境,对实时监控和日志分析的需求变得日益迫切。本文将介绍如何使用Golang编写实时日志分析模块,以增强公司监控管理软件的日志监控功能。通过本文的指导,你将能够建立一个定制的实时日志分析系统,更好地监测和管理公司的网络活动。
225 0
|
5月前
|
消息中间件 监控 关系型数据库
134 日志监控告警系统案例(功能架构分析)
134 日志监控告警系统案例(功能架构分析)
72 0
|
8月前
|
Java API 数据安全/隐私保护
一张思维导图带你学会SpringBoot使用AOP实现日志管理功能
一张思维导图带你学会SpringBoot使用AOP实现日志管理功能
119 0
|
8月前
|
监控 数据可视化 安全
Baumer工业相机堡盟相机如何使用Trace功能(相机日志追踪的使用和优点以及行业应用)(C++)
Baumer工业相机堡盟相机如何使用Trace功能(相机日志追踪的使用和优点以及行业应用)(C++)
70 0