ASP.NET核心中的WCF跟踪

问题描述 投票:4回答:2

我们曾经在ASP.NET上使用WCF,最近通过ASP.NET Core切换到WCF。这很难实现,因为ASP.Net Core不支持开箱即用的WCF。首先,整个web.config XML配置模型已被转储到ASP.NET Core中,因此我们无法在那里配置WCF跟踪。

即这个文件没用:https://docs.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/tracing/configuring-tracing

我们不得不通过在WCF和端口80之间放置一个http代理来破解ASP.NET Core.WCF实际上是在另一个端口上运行。

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

c# asp.net .net wcf asp.net-core
2个回答
1
投票

您将在.NET Core上使用ETW Tracing for WCF

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

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

  1. 所有WCF应用程序都在进行跟踪,而不是通过配置文件配置单个应用程序
  2. 您无法使用ETW跟踪输出消息
  3. SvcTraceViewer.exe无法用于跟踪审查,您需要转移到可能呈现学习曲线的PerfView.exe

ETW的好处

  1. 您可以避免经典形式的跟踪造成的性能损失
  2. 没有更多的配置更改来启动/停止跟踪

1
投票

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

客户初始化:

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

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)
        {
        }
    }

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;
        }
    }

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

© www.soinside.com 2019 - 2024. All rights reserved.