I would like to trace performance inside a c# mobile application, in order to know the amount of time needed by some long-running methods. The goal is to know the performance at the level of methods. This tracing would be decided at runtime.
First idea that came is to maintain a global variable “doWeHaveToTrace”, to check it each time a potentially traceable method is called, and trace it if that variable is true. And in this case, I probably would use a Logger or a TraceListener to write tracing infos into a file.
But I think this mechanism is quite heavy, because a variable has to be checked in each method, and the logger calls have to be coded.
I also thought about aspect programming (insert tracing code in before() and after() methods), but I don’t think aspect programming could be allowed inside this project.
So I was wondering, is there any other good mechanisms that could provide such performance tracing ? Is it possible to trace a whole method ? And to decide to stop or resume tracing at runtime ?
Not sure exactly what you are trying to trace, but if you’re trying to track time between long running methods, perhaps you’d be better off hooking into the .NET Profiling API. Here is a tutorial on usage.
A commerical profiler would be another, simpler option.