Sanket Vaidya
Sanket Vaidya

Reputation: 1

How to log retry counts when using Azure SDK retry policy

I have added retry policy to connect blob

BlobClientOptions blobOptions = new BlobClientOptions()
            {
                Retry =
                {
                    Delay = TimeSpan.FromSeconds(5),
                    MaxRetries = 5,
                    Mode = RetryMode.Exponential,
                    MaxDelay = TimeSpan.FromSeconds(5),
                    NetworkTimeout = TimeSpan.FromSeconds(5)
                }
            };

            

 BlobServiceClient blobServiceClient = new BlobServiceClient(blobConnectionString,blobOptions);

But how can I log that how may retry counts happened while connecting blob.Since in instance of BlobServiceClient, I am not getting any options to check the Retry count.

I am trying to log the count of retries made during connection to the azure blob and how to reproduce scenarios where I can log multiple counts.

Upvotes: 0

Views: 317

Answers (1)

Sampath
Sampath

Reputation: 3669

I used this MSDOC to set up telemetry with Application Insights and stack reference.

 static int disconnectCount = 0;
    static BlobServiceClient? blobServiceClient;
    static TelemetryClient telemetryClient = new TelemetryClient();

    static void Main(string[] args)
    {
        
        Trace.Listeners.Add(new ConsoleTraceListener());

        int n = 500; 

        for (int i = 0; i < n; i++)
        {
            ConnectToBlobStorage();
    

            DisconnectFromBlobStorage();
        }

        Console.WriteLine($"Total Connections: {connectCount}");
        Console.WriteLine($"Total Disconnections: {disconnectCount}");
    }

    static void ConnectToBlobStorage()
    {
        try
        {
            blobServiceClient = new BlobServiceClient("connection string");
            Trace.WriteLine("Connected to Azure Blob Storage");
            connectCount++;
            telemetryClient.TrackTrace("Connected to Azure Blob Storage");
            
        }
        catch (Exception ex)
        {
            Trace.WriteLine($"Error connecting to Azure Blob Storage: {ex.Message}");
            telemetryClient.TrackException(ex);
        }
    }

    static void DisconnectFromBlobStorage()
    {
        try
        {
            if (blobServiceClient != null)
            {
                blobServiceClient = null;
                Trace.WriteLine("Disconnected from Azure Blob Storage");
                disconnectCount++;
                telemetryClient.TrackTrace("Disconnected from Azure Blob Storage");
            }
        }
        catch (Exception ex)
        {
            Trace.WriteLine($"Error disconnecting from Azure Blob Storage: {ex.Message}");
            telemetryClient.TrackException(ex);
        }

Output:

enter image description here

enter image description here

Upvotes: 0

Related Questions