I'm trying to find the best way to measure the duration of a method to log them on Application Insights, I know it's possible if we do something like this:
public void TestMethod()
{
var sw = Stopwatch.StartNew();
//code here
sw.Stop();
Console.WriteLine("Time elapsed: {0}", sw.Elapsed);
}
But, as you suppose, I don't want to write it on all the methods, ideally, I want to use a decorator, something similar to this.
[MeasureTime]
public void TestMethod()
{
//code here
}
Or something similar. So my question is: How can I build something like this? Is there a better way to do it?
Thanks in advance!