Shailendra
Shailendra

Reputation: 173

How can I get the count of Azure function execution programmatically in c#?

I have timer trigger azure function which runs on every minute. Is it possible to get the count of how many time the function executed in my Azure function c# code at every time function execute?

PS: I need this count to generate a sequential counter.Also with HTTP trigger function too! Is it possible with Application Insights -> metrics with function count filter. In a way we get this count in our code ?

Upvotes: 0

Views: 3251

Answers (2)

George Chen
George Chen

Reputation: 14324

Maybe you could implement it like the below picture, the variable will be shared. However if you restart your function, it will reload the variable time.

enter image description here

In case of the function restart, suppose you could set it with a storage queue to implement it, every time add 1 to the queue number and replace the queue.

public static async void Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));
            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
            CloudQueue queue = queueClient.GetQueueReference("time");
            CloudQueueMessage message = queue.GetMessage();
            log.LogInformation(message.AsString);
            int num = int.Parse(message.AsString);
            num++;
            queue.DeleteMessage(message);
            queue.AddMessage(new CloudQueueMessage(num.ToString()));

        }

enter image description here

Hope this could help you, if you still have other problem please feel free to let me know.

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222582

It is not possible to get the execution times and there is no API to do that, you need to manually maintain the count in a datastore and fetch it using custom logic.

Upvotes: 0

Related Questions