代码之家  ›  专栏  ›  技术社区  ›  Sten Petrov

服务结构QoS日志记录

  •  0
  • Sten Petrov  · 技术社区  · 6 年前

    我有一个微服务应用程序,我想记录服务质量(QoS)日志-每个服务调用一个日志行,无论服务调用是什么。

    我不想更改服务方法的签名以获取更多参数。相反,我希望以某种方式更改远程处理堆栈,以便涵盖所有当前和未来的服务调用。

    answer 写了一篇 IServiceRemotingClient 使用 AsyncLocal 存储上下文信息。例如,这不允许我检查响应类型和日志异常堆栈跟踪。

    有没有一种方法可以将SF的调用封装到更接近我的服务方法的方法中,在那里我可以记录异常和结果,而不是在远程处理级别,那里所有的东西都已经打包为一个黑盒

    public class LoggingServiceRemotingClient: IServiceRemotingClient
    {
       IServiceRemotingClient standardClient;
       ...
       public async Task<IServiceRemotingResponseMessage> RequestResponseAsync(IServiceRemotingRequestMessage requestMessage)
       {
         var callId = Guid.NewGuid();
         try{
           SetCallIdInAsyncLocal(callId); // trimmed down pseudo code
           var response = await this.standardClient.RequestResponseAsync(requestMessage);
           Log.Ok(callId); // trimmed down pseudo code
         } catch (Exception x){
           // this will never be hit because the response body was already created with an exception serialized in it.
           // how do I catch such exceptions across all calls?
           // also some calls a due to client errors, others are internal - a single place to differentiate would be nice.
           Log.Fail(callId, x);
         }
       }
    }
    

    callId 要记录自己的进程:

    public class MyService: StatelessService, IMyService
    {
      public async Task<string> GetMeAString(string prefix)
      {
          Debug.Assert(prefix!=null); // this exception is a caller fault
          Guid callId = GetFromAsyncLocal();
          Log(callId, "Got a call here")
          string result = prefix+": "+Guid.NewGuid().ToString();
          Log(callId, "returning "+result);
          return result;
      }
    }
    

    我不明白为什么 异步本地 在这里有效,但很明显,确实有效。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Diego Mendes    6 年前

    我想你在找 Trace Correlation with Service Remoting 用于服务结构的Microsoft应用程序洞察 包裹。

    它会将所有依赖项跟踪信息发送到application insights,不需要在代码中做太多更改,还可以与其他applicationinsights监视功能结合使用。

    如果您想从头开始或不使用AppInsights,Peter Bons提到的选项也是有效的。