can Log method of class A get to know who calls it ?
class A
{
public void Log(string msg)
{
Log.Write("method_name: " + msg);
}
}
I want to know a name of class and a name of a method.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the
StackTraceandStackFrameclasses. You can either get a whole stack trace by calling theStrackTraceconstructor, or just a particular stack frame using theStackFrameconstructor which takes the number of frames to skip.You should be aware that it can be inaccurate due to inlining. (e..g method A inlines method B which calls your method – method A will be reported, not B).
Sample code:
Running as an optimized release build, this will print
Void Main()– when running a debug build, or if you put more code into theIntermediate()method, it will printVoid Intermediate().(As mentioned in comments, this will also create a performance hit. You should measure it to see if it’s acceptable in your particular case.)