0

Possible Duplicate:
How can I add a Trace() to every method call in C#?

In the following example, Im inserting debug info inside each methods so that I can see which function is called:

namespace A
{
    class A1
    {
        void f1()
        {
            Console.WriteLine("inside A.A1.f1");  //1
        }
        void f2()
        {
            Console.WriteLine("inside A.A1.f2");  //2
        }
    }
 }

There are hundreds of methods under different namespaces and classes.
Inserting debug messages one by one into each method is cumbersome and waste of time.
Instead of inserting method specific debug messages as above is there better way to do it?
Maybe an API function that can tell which method Im in?

Community
  • 1
  • 1
Chris
  • 259
  • 1
  • 4
  • 14
  • because the project is open source and had some need to tweak it. – Chris Jan 03 '13 at 01:21
  • You might find this question and answer useful: http://stackoverflow.com/questions/5999177/for-c-sharp-logging-how-to-obtain-call-stack-depth-with-minimal-overhead – RenniePet Jan 03 '13 at 01:25
  • @RenniePet: maybe close. but not sure what he is trying to do... – Chris Jan 03 '13 at 02:46

2 Answers2

1

StackTrace and StackFrame class may provide information you need (note that inlined methods will not get stack frames):

catch (Exception e)
{
  StackTrace st = new StackTrace();
  StackTrace st1 = new StackTrace(new StackFrame(true));
  Console.WriteLine(" Stack trace for Main: {0}",
    st1.ToString());
  Console.WriteLine(st.ToString());
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    would this work for multi-threaded environment? – Chris Jan 03 '13 at 01:34
  • @Chris, in what sense? StackTrace/StackFrame give you stack of current thread. You don't want to explain what information you are looking for so I don't know if it even give you any details you are need or "works for you" in multi-threaded case. – Alexei Levenkov Jan 03 '13 at 02:11
  • StackTrace and StackFrame seem being used inside catch{}. When no exception thrown they never be triggered, right? – Chris Jan 03 '13 at 02:44
  • @Chris you can use them whenever you want... The sample is copy-pasted from the articles I've linked. – Alexei Levenkov Jan 03 '13 at 02:51
1

If you were using VS 2012, I would tell you to use Caller Information in your WriteLine.

Unfortunately in VS2010 this is not possible. You could call something like this but it would really not be performant:

namespace A
{
    public static class DebugHelper
    {
        [Conditional("DEBUG")]
        public static void LogMethodName()
        {
            var method = new StackFrame(1).GetMethod();
            Console.WriteLine("inside {0}.{1}", method.DeclaringType, method.Name);
        }
    }
    class A1
    {
        void f1()
        {
            DebugHelper.LogMethodName();  //1
        }
        void f2()
        {
            DebugHelper.LogMethodName();  //2
        }
    }
 }
Marcel Gosselin
  • 4,610
  • 2
  • 31
  • 54
  • Hi Marcel, "not be performant" in terms of performance? – Chris Jan 03 '13 at 02:58
  • 1
    Look at [this thread](http://stackoverflow.com/q/1348643/197371) and evaluate if the small performance hit is better than the risk of passing the wrong method or class name when doing copy-paste between methods. – Marcel Gosselin Jan 03 '13 at 03:31
  • Caller Information seems to be .Net4.5 feature. need to recompile my solution and insert WriteLine() into each methods to show the CallerFilePath attribute. – Chris Jan 03 '13 at 10:03
  • 1
    Caller Information is a **C#/.Net4.5** feature, that is why I said you would need VS2012. Be careful with these attributes as they get the file/method names of the **calling** method, not of the current one. – Marcel Gosselin Jan 03 '13 at 15:26