【问题标题】:WCF client logging dotnet coreWCF 客户端日志记录 dotnet 核心
【发布时间】:2016-12-16 13:46:42
【问题描述】:

我在 Windows 上使用 asp.net core,并且有一个文件,其中包含由 dotnet-svcutil 生成的类。我正在使用 nlog 进行日志记录。有没有一种方法可以记录所有原始请求和响应到外部服务和来自外部服务?

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

【问题讨论】:

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


    【解决方案1】:

    在这里找到答案: https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/how-to-inspect-or-modify-messages-on-the-client

    动作顺序:

    1. 实现System.ServiceModel.Dispatcher.IClientMessageInspector接口。 您可以在此处检查/修改/记录消息
    2. 根据您要插入客户端消息检查器的范围实现 System.ServiceModel.Description.IEndpointBehaviorSystem.ServiceModel.Description.IContractBehaviorSystem.ServiceModel.Description.IEndpointBehavior 允许您在端点级别更改行为。 System.ServiceModel.Description.IContractBehavior 允许您在合同级别更改行为。
    3. 在调用 ClientBase.OpenSystem.ServiceModel.ChannelFactory 上的 ICommunicationObject.Open 方法之前插入行为。李>

    【讨论】:

      【解决方案2】:

      行为:

      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>());
      

      【讨论】:

      • 这个答案的用法部分对我很有帮助。我会注意到,您还可以像这样将类添加到 EndpointBehaviors:client.Endpoint.EndpointBehaviors.Add(new LoggingEndpointBehaviour()) 假设您正在实现的类具有无参数构造函数。
      • 我过去三个小时都在试图捕捉我的请求和响应。做了所有建议的事情。特别是消息检查员。但是由于解释不佳,除了您的解释之外,它们都没有奏效。你真的拯救了我的一天。
      猜你喜欢
      • 2011-03-18
      • 1970-01-01
      • 2016-05-22
      • 2013-11-11
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多