11

I've recently been tasked with adding logging statements to every method call in a solution. These log entries need to contain the class and method name.

I know I can use MethodBase.GetCurrentMethod() and the StackFrame.GetMethod() methods. Which is better? Is there a better (or more performant) way to get the class and method name?

4
  • 6
    Every method? There are easier ways to make your app ten thousand times slower or to generate a gigabyte of text a minute. Commented Aug 25, 2010 at 16:56
  • 5
    My lot in life is only to do what the client asks. Commented Aug 25, 2010 at 17:02
  • 1
    there's a place in the consulting relationship for constructive criticism and, in fact, it's an important part of the job. I've worked on projects that had this kind of pervasive trace logging and it's generally resulted in everybody ignoring the logs unless they absolutely had to take the several hours it would require to dig through them. Commented Aug 25, 2010 at 17:22
  • @Dan, I mentioned it. I've had generally the same experience with this kind of logging. Usually needing it is indicative of a much deeper design issue (which this project definitely has). The overriding factor is that you have to take the most efficient path to shipping the software. I hate to admit it, but in this case, the client is right. Commented Aug 25, 2010 at 18:27

4 Answers 4

11

Well, the best/fastest way is to include a string in every function. That may not appear the most practical solution, but MethodBase.GetCurrentMethod() requires coding inside every method that using it anyway. i.e. You can write

string funcName = "MyClass.MyFunction(int, int)";

or you can write

string funcName = MethodBase.GetCurrentMethod().Name

Now, if you want to get the Name of the function that called the current function (i.e., you want to do this in one spot in your logging function), then your only option is reading through the StackFrame.

Sign up to request clarification or add additional context in comments.

2 Comments

If I recall right, MethodBase.GetCurrentMethod() might also return different results based on any inlining/optimization done by the compiler, so hardcoding function names may be more reliable in that sense.
you can automate including function name as string at build process
5

I have two suggestions:

  1. Use ready-made AOP frameworks (like http://www.sharpcrafters.com/ ) they can handle this easily
  2. Do a custom prebuild action where you replace some kind of stub in the beginning of every method:

    void PerformSomeAction() { //PUT_LOGGING_HERE }

then in custom tool replace those stubs with method names. This is guaranteed fastest method, but requires some investments.

1 Comment

Would that I could, I've already brought up that suggestion. Not that it isn't an excellent point.
1

Since it hasn't been mentioned yet:
If you want the string name of a method (or class, or variable) just use nameof(MyMethod).
The nameof expression is evaluated at compile time to a const string, so this is the most performant solution since there is no computation at runtime.
This is not a solution if the name of the method should be retrieved from within another class (e.g. in the logger).

Comments

0

this.getType().toString() should get you the class

About the method it seems stackFrame and methodbase are the most obvouis solutions, I cant comment on which is more efficient.

2 Comments

Should it be this.GetType().ToString() ? or am I missing something else
Object.ToString() will return the class name unless ToString() was overridden.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.