I using C# and woundering if it possible to create a custom attribute that will measuring execution time of code.
I'll explain. Let's say that you have the class Task
:
public class Task
{
public void Run()
{
Do1();
Do2();
// ...
}
public void Do1()
{
// ...
Thread.sleep(10); // Dummy
}
public void Do2()
{
// ...
Thread.sleep(5); // Dummy
}
}
And the main file:
public static void Main()
{
Task task = new Task();
task.Run();
}
Now, I running this code on my server and feel that its running too slow (then I expected). So I using my brand new attribute to measuring execution time - let's call it MeasuringTimeAttribute
:
[MeasuringTime]
public void Run()
{
// ...
}
When running the code again, I want it to generate a report that looks like this:
Run() - Took 15s
Run().Do1() - Took 10s
Run().Do2() - Took 5s
The basic idea is that it will measuring for my code even if I have more then 2 levels (like in this example). Also, no matter what code it's running on. All I have to do is to add the [MeasuringTime]
attribute the entry-point function.
Is this possible? If yes, I will appreciate every tip I'll get in order to implement it.