ASP.NET Core中的WCF跟踪

Mel*_*per 5 .net c# asp.net wcf asp.net-core

我们曾经使用过ASP.NET上的WCF,最近又切换到了ASP.NET Core上的WCF。这很难实现,因为ASP.Net Core不支持WCF。一方面,整个web.config XML配置模型已转储到ASP.NET Core中,因此我们无法在此处配置WCF跟踪。

即该文档无用:https : //docs.microsoft.com/zh-cn/dotnet/framework/wcf/diagnostics/tracing/configuring-tracing

我们必须通过在WCF和端口80之间放置一个http代理来入侵ASP.NETCore。WCF实际上在另一个端口上运行。

问题是,如果ASP.NET Core不关注web.config,如何启用WCF跟踪?

Pet*_*kiy 12

在客户端跟踪的情况下,我使用自定义端点行为 ( IEndpointBehavior) 和自定义消息日志检查器 ( IClientMessageInspector) 来获取输入和输出消息。

客户端初始化:

_serviceClient = new MyCustomServiceClient();
_serviceClient.Endpoint.Address = new System.ServiceModel.EndpointAddress(_configParams.ServiceUri);
_serviceClient.Endpoint.EndpointBehaviors.Add(new EndpointLoggingBehavior("MyCustomService"));
Run Code Online (Sandbox Code Playgroud)

实施EndpointLoggingBehavior

public class EndpointLoggingBehavior : IEndpointBehavior
    {
        public EndpointLoggingBehavior(string serviceName)
        {
            _serviceName = serviceName;
        }

        private readonly string _serviceName;

        public void AddBindingParameters(ServiceEndpoint endpoint,
            System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.ClientMessageInspectors.Add(new MessageLoggingInspector(_serviceName));
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }
Run Code Online (Sandbox Code Playgroud)

实施MessageLoggingInspector

public class MessageLoggingInspector : IClientMessageInspector
    {
        private readonly string _serviceName;
        public MessageLoggingInspector(string serviceName)
        {
            _serviceName = serviceName;
        }
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            // copying message to buffer to avoid accidental corruption
            var buffer = reply.CreateBufferedCopy(int.MaxValue);
            reply = buffer.CreateMessage();
            // creating copy
            var copy = buffer.CreateMessage();
            //getting full input message
            var fullInputMessage = copy.ToString();

        }
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            // copying message to buffer to avoid accidental corruption
            var buffer = request.CreateBufferedCopy(int.MaxValue);
            request = buffer.CreateMessage();
            // creating copy
            var copy = buffer.CreateMessage();
            //getting full output message
            var fullOutputMessage = copy.ToString();
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后,当然,您需要将这些消息写入任何存储。


小智 2

您将在 .NET Core 上使用 ETW 跟踪 WCF

https://github.com/dotnet/wcf/blob/master/Documentation/HowToUseETW.md

根据我的经验,你有一些限制

  1. 对所有 WCF 应用程序启用跟踪,而不是通过配置文件为单个应用程序进行配置
  2. 您无法使用 ETW 跟踪输出消息
  3. SvcTraceViewer.exe 不能很好地进行跟踪审查,您需要迁移到 PerfView.exe,这可能会带来学习曲线

ETW 的好处

  1. 您可以避免经典跟踪形式对性能的影响
  2. 不再需要更改配置来启动/停止跟踪