使用流利的 API 跟踪异步 Azure 操作

Track asynchronous Azure operations using the fluent API

我知道您可以使用标准 API 跟踪正常操作:https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-async-operations

但是我想知道是否有一种已知的方法可以利用 Fluent Azure Management Libraries 来跟踪长时间的异步操作,例如 VM 操作等。例如,VM 重启方法是一个无效任务,它不会 return 用于跟踪的操作 ID。

async Task IVirtualMachineScaleSetVM.RestartAsync(CancellationToken cancellationToken)
{
  await this.RestartAsync(cancellationToken);
}

干杯!

AFAIK,似乎很难跟踪没有 return operationId 的 VM 重启状态。

登录 .NET 的流畅 Azure 管理库利用底层 AutoRest 服务客户端跟踪。

创建一个实现 Microsoft.Rest.IServiceClientTracingInterceptor 的 class。 class 将负责拦截日志消息并将它们传递给您正在使用的任何日志记录机制。

class ConsoleTracer : IServiceClientTracingInterceptor
{
    public void ReceiveResponse(string invocationId, HttpResponseMessage response) { }
}

在创建 Microsoft.Azure.Management.Fluent.Azure 对象之前,通过调用 ServiceClientTracing.AddTracingInterceptor() 初始化上面创建的 IServiceClientTracingInterceptor 并将 ServiceClientTracing.IsEnabled 设置为 true.创建 Azure 对象时,包括 .WithDelegatingHandler().WithLogLevel() 方法以将客户端连接到 AutoRest 的服务客户端跟踪。

ServiceClientTracing.AddTracingInterceptor(new ConsoleTracer());
ServiceClientTracing.IsEnabled = true;

var azure = Azure
    .Configure()
    .WithDelegatingHandler(new HttpLoggingDelegatingHandler())
    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
    .Authenticate(credentials)
    .WithDefaultSubscription();

更多细节,你可以参考这篇article