'Log execution time of Activity Functions
I have a durable function with multiple activity functions. I need to log the execution time of each activity function. This is how I currently calculate:
using System.Diagnostics;
namespace Hosts
{
public class ActivityFunctionOne
{
private readonly Stopwatch _stopwatch;
public ActivityFunctionOne()
{
_stopwatch = new Stopwatch();
}
[FunctionName("ActivityFunctionOne")]
public async Task Read([ActivityTrigger])
{
_stopwatch.Start();
await fun.ReadAsync();
_stopwatch.Stop();
// log ("completed in {_stopwatch.ElapsedMilliseconds}");
}
}
}
using System.Diagnostics;
namespace Hosts
{
public class ActivityFunctionTwo
{
private readonly Stopwatch _stopwatch;
public ActivityFunctionTwo()
{
_stopwatch = new Stopwatch();
}
[FunctionName("ActivityFunctionTwo")]
public async Task Write([ActivityTrigger])
{
_stopwatch.Start();
await fun.WriteAsync();
_stopwatch.Stop();
// log ("completed in {_stopwatch.ElapsedMilliseconds}");
}
}
}
As you can see, I have added stopwatch in each Activity function to log the execution time. Is there a way to implement stopWatch in one location and calculate the execution time of each activity function without this repetition? Also, if I add new activity functions later on, is it possible to calculate execution time of those activity functions as well without adding these lines?
Solution 1:[1]
Im not aware of a clean way to log the execution with the ilogger instance without the stopwatch.start() and stopwatch.stop() in each activity function. However the execution time is actually already logged in appinsights for all activities which should probably be sufficent information for most kinds of performance debugging. A simple query to get it for a function would be.
traces
| where customDimensions['prop__functionName']=='SomeActivityFunctionName'
| where customDimensions['EventName'] == 'FunctionCompleted'
| project timestamp, customDimensions['prop__executionDuration']
Solution 2:[2]
Currently, it is not possible. A single stopwatch process that uses each Activity function to log the execution time.
Anyway, we need to add the Stopwatch.Start() which is used for start the stopwatch to know when the specific task can start. And the Stopwatch.Stop() is used to stop the stopwatch to know the exact time for the specific task can be done, So we need to add those two lines before starting and after ending the specific task.
Stopwatch _stopwatch = new Stopwatch();
_stopwatch.Start();
// your task to know the execution time.
_stopwatch.Stop();
Refer here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bjorne |
| Solution 2 |
