Hello. I'd like for debugging purposes to be able to log what functions are called and in what order. So I've been just putting Debug.WriteLine("myFunctionName(args)") all over my functions, logging it in the end to a file. Isn't there a better approach to do this? I'd hope so.
Look at Aspect Oriented Programming.
You can use PostSharp to implement AOP in your C# code. E.g.
public class TraceAttribute : OnMethodBoundaryAspect
{
public override void OnEntry( MethodExecutionEventArgs eventArgs)
{ Trace.TraceInformation("Entering {0}.", eventArgs.Method); }
public override void OnExit( MethodExecutionEventArgs eventArgs)
{ Trace.TraceInformation("Leaving {0}.", eventArgs.Method); }
}
Now you can decorate your methods with 'TraceAttribute' and you will be able to log details about when the method is called.
If you only need it for a few places to look at things you could use a StackTrace.
StackTrace stack = new StackTrace(true);
string message = stack.ToString();
Of course this would not keep track of everything, but I have found it useful.
There is also a component in the Enterprise Library from Microsoft, Policy Injection Application Block, that will allow you to add things like logging to the the entry and exit of methods as well. I saw a nice demo of this for this type of thing.
Policy Injection Application Block
Minimizing the work required and the code that the developer must write to perform common tasks within an application, such as logging, validation, authorization, and instrumentation
If you don't want anything invasive in your code, then you have to go the profiling/trace way. Some profilers will give you a complete overview of the method calls, stacks and execution run throughout the application life-time.
Disclaimer: I am a developer on this solution.
There is also a commercially available solution that will allow you to inject usage tracking directly into your application binaries, similar PostSharp's IL Weaving. The product is Runtime Intelligence from PreEmptive Solutions and we are using Dotfuscator as a code injection tool to add application usage tracking functionality to .NET applications.
Since it works by injecting new code directly into your compiled assemblies you can use the Dotfuscator user interface to select which methods whose usage you want to track so that you don't even need to add custom attributes to your code (although we also provide a set of custom attributes so that you can do so if you want). This is not typical logging, as all usage data is sent via HTTP to a central data collection server (by default, one hosted at PreEmptive) but the added value is that we provide a great deal of analysis and reporting about the usage of your applications. You can also implement your own endpoint to receive the usage data although we have not yet released a sample for that.
Basic instrumentation functionality (which will allow you to instrument up to 10 methods per application) is currently available for free in Visual Studio 2010 (out in Beta 1 now) in Dotfuscator Software Services - Community Edition. In addition we provide a free data collection and reporting portal to go along with the VS 2010 release so you can try out adding usage tracking to your applications anonymously and for free.
I have started a series of blog posts detailing this solution: What is Runtime Intelligence and Correlating Download to Usage With Visual Studio 2010 . For a quick overview of all that is in Visual Studio 2010 see: What's New With Dotfuscator In Visual Studio 2010
You can also contact PreEmptive to get a free time-limited fully functional evaluation so that you can instrument an unlimited number of methods as well as much more advanced functionality.