Application Method Tracing

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

I have inherited an application that I am not very familiar with and I would
like to know if there is a way to produce a trace of what methods were
called and the order in which they executed for particilar instances.
Nothing fancy just something that would maybe log each method to a file as
it executes.

Thanks,

Matt
 
Hi Matt,

What do you actually mean by "for particular instances"? - are you referring
to specific objects?

AFAIK, there is no direct way to log each method as it executes. So in other
words, you will have to add a code statement in each method to log it.

However, you could make use of the stack trace to walk back and find out
which methods were executed. You need to use the StackTrace.GetFrame method
for that. Check out http://rakeshrajan.com/blog/archive/2005/05/02/16.aspx
for an example.
 
Matt,

If you are willing to place a line of code in each method, see the Debug or
Trace classes in the System.Diagnostics namespace. Also, take a look at the
System.Diagnostics.TextWriterTraceListener class, which can be used by
either the Debug or Trace classes to dump the trace to a file as the program
executes.

Here is an example for the help file:
public static int Main(string[] args)
{
// Create a file for output named TestFile.txt.
Stream myFile = File.Create("TestFile.txt");

/* Create a new text writer using the output stream, and add it to
* the trace listeners. */
TextWriterTraceListener myTextListener = new
TextWriterTraceListener(myFile);
Trace.Listeners.Add(myTextListener);

/* Create a text writer that writes to the console screen, and add
* it to the trace listeners */
TextWriterTraceListener myWriter = new
TextWriterTraceListener(System.Console.Out);
Trace.Listeners.Add(myWriter);

// Write output to the file and to the console screen.
Trace.Write("Test output ");

// Write only to the console screen.
myWriter.WriteLine("Write only to the console screen.");

// Flush and close the output.
Trace.Flush();
myWriter.Flush();
myWriter.Close();

return 0;
}


Hope this helps,
Dave
 
Back
Top