How can I get the name of the calling method?
-
In my logging code, I want to display the name of the method that wrote to the log. How can my Log method get the name of the method that called it? I'm thinking of something like the .TargetSite of an Exception (except I'd prefer not to throw an exception :-)... unless that's how to do this).
-
In my logging code, I want to display the name of the method that wrote to the log. How can my Log method get the name of the method that called it? I'm thinking of something like the .TargetSite of an Exception (except I'd prefer not to throw an exception :-)... unless that's how to do this).
Let me make this more generic: - how, in code, can I stop and get the stack trace up to that line of code? Hopefully I can do this without stopping and throwing an exception and reading the stack trace from the Exception object. I'm sure that's a performance hit...
-
Let me make this more generic: - how, in code, can I stop and get the stack trace up to that line of code? Hopefully I can do this without stopping and throwing an exception and reading the stack trace from the Exception object. I'm sure that's a performance hit...
By using System.Diagnostics.StackFrame like this: public static void Main() { Func(); RL(); } public static void Func() { //Get stack with line numbers System.Diagnostics.StackTrace Stack = new System.Diagnostics.StackTrace(true); foreach(System.Diagnostics.StackFrame Frame in Stack.GetFrames()) { WL(Frame); } }
-
By using System.Diagnostics.StackFrame like this: public static void Main() { Func(); RL(); } public static void Func() { //Get stack with line numbers System.Diagnostics.StackTrace Stack = new System.Diagnostics.StackTrace(true); foreach(System.Diagnostics.StackFrame Frame in Stack.GetFrames()) { WL(Frame); } }
Thanks. That is exactly what I was looking for. Unfortunately, I never got email about your reply! So I went through the Help files and found this myself. I was just coming back to answer my own question when I saw your reply. However, I'm curious how expensive *this* is, too...