服务结构QoS日志记录

时间:2018-12-12 01:34:05

标签: .net azure-service-fabric

我有一个微服务应用程序,我想在其中记录服务质量(QoS)日志-每个服务调用一个日志行,无论该服务调用是什么。 我还希望每个呼叫都具有详细的呼叫,这些呼叫在QoS日志和详细的日志中具有相同的ID。

我不想更改服务方法的签名以接受更多参数。而是我想以某种方式更改远程处理堆栈,以便涵盖当前和将来的所有服务调用。

我遵循了这个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;
  }
}

我不明白为什么AsyncLocal在这里起作用,但显然可以。

1 个答案:

答案 0 :(得分:1)

我认为您正在从 Microsoft Application Insights for Service Fabric 软件包中寻找Trace Correlation with Service Remoting

它将所有依赖项跟踪信息发送到应用程序见解,不需要对代码进行太多更改,还可以与其他应用程序见解监视功能结合使用。

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