WCF客户端记录dotnet核心

时间:2016-08-10 09:10:50

标签: c# wcf soap asp.net-core svcutil.exe

我在Windows上使用 asp.net核心并拥有一个由 dotnet-svcutil 生成的类的文件。我使用nlog进行日志记录。有没有办法可以将所有原始请求和响应记录到外部服务中?

已经尝试过logman https://github.com/dotnet/wcf/blob/master/Documentation/HowToUseETW.md,但首先 - 它没有显示原始肥皂,只显示事件,第二 - 我需要配置的nlog记录日志。

2 个答案:

答案 0 :(得分:4)

行为:

public class LoggingEndpointBehaviour : IEndpointBehavior
{
    public LoggingMessageInspector MessageInspector { get; }

    public LoggingEndpointBehaviour(LoggingMessageInspector messageInspector)
    {
        MessageInspector = messageInspector ?? throw new ArgumentNullException(nameof(messageInspector));
    }

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

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

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

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

检查员:

 public class LoggingMessageInspector : IClientMessageInspector
{
    public LoggingMessageInspector(ILogger<LoggingMessageInspector> logger)
    {
        Logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
    }

    public ILogger<LoggingMessageInspector> Logger { get; }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        using (var buffer = reply.CreateBufferedCopy(int.MaxValue))
        {
            var document = GetDocument(buffer.CreateMessage());
            Logger.LogTrace(document.OuterXml);

            reply = buffer.CreateMessage();
        }
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        using (var buffer = request.CreateBufferedCopy(int.MaxValue))
        {
            var document = GetDocument(buffer.CreateMessage());
            Logger.LogTrace(document.OuterXml);

            request = buffer.CreateMessage();
            return null;
        }
    }

    private XmlDocument GetDocument(Message request)
    {
        XmlDocument document = new XmlDocument();
        using (MemoryStream memoryStream = new MemoryStream())
        {
            // write request to memory stream
            XmlWriter writer = XmlWriter.Create(memoryStream);
            request.WriteMessage(writer);
            writer.Flush();
            memoryStream.Position = 0;

            // load memory stream into a document
            document.Load(memoryStream);
        }

        return document;
    }
}

用法:

 if (configuration.GetValue<bool>("Logging:MessageContent"))
     client.Endpoint.EndpointBehaviors.Add(serviceProvider.GetRequiredService<LoggingEndpointBehaviour>());

答案 1 :(得分:-1)

在这里找到答案: https://msdn.microsoft.com/en-us/library/ms733786.aspx

行动顺序:

  
      
  1. 实现 System.ServiceModel.Dispatcher.IClientMessageInspector 接口。 您可以在这里检查/修改/记录消息
  2.   
  3. 实现 System.ServiceModel.Description.IEndpointBehavior System.ServiceModel.Description.IContractBehavior ,具体取决于您要插入客户端消息检查器的范围。 System.ServiceModel.Description.IEndpointBehavior 允许您更改端点级别的行为。 System.ServiceModel.Description.IContractBehavior 允许您更改合同级别的行为。
  4.   
  5. System.ServiceModel.ChannelFactory 上调用 ClientBase.Open ICommunicationObject.Open 方法之前插入行为。
  6.