Search code examples
azureazure-api-managementazure-management-apiazure-fluent-api

Track asynchronous Azure operations using the fluent API


I know you can track normal operations using the standard API: https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-async-operations

However I was wondering if there was a known way to utilize the Fluent Azure Management Libraries to track long async operations such as VM operations etc. For example the VM restart method is a void Task which does not return an operation-id for tracking.

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

Cheers!


Solution

  • AFAIK, it seems that it's hard to trace VM restart status which does not return an operationId.

    Logging in the fluent Azure management libraries for .NET leverages the underlying AutoRest service client tracing.

    Create a class that implements Microsoft.Rest.IServiceClientTracingInterceptor. This class will be responsible for intercepting log messages and passing them to whatever logging mechanism you're using.

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

    Before creating the Microsoft.Azure.Management.Fluent.Azure object, initialize the IServiceClientTracingInterceptor you created above by calling ServiceClientTracing.AddTracingInterceptor() and set ServiceClientTracing.IsEnabled to true. When you create the Azure object, include the .WithDelegatingHandler() and .WithLogLevel() methods to wire up the client to AutoRest's service client tracing.

    ServiceClientTracing.AddTracingInterceptor(new ConsoleTracer());
    ServiceClientTracing.IsEnabled = true;
    
    var azure = Azure
        .Configure()
        .WithDelegatingHandler(new HttpLoggingDelegatingHandler())
        .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
        .Authenticate(credentials)
        .WithDefaultSubscription();
    

    For more details, you could refer to this article.