.NET: Is there a way to know which method the currently executing code is in? (for logging class)

I'm writing a logging class in C# and would like to add the method from which the log call was made. Doing this manually isn't too attractive. Is there a way to know which method the currently executing code is in?

Thanks in advance for your awesomeness...


EDIT: Using MethodBase...

 System.Reflection.MethodBase thisMethod = System.Reflection.MethodBase.GetCurrentMethod();
 Console.WriteLine("This method is: " + thisMethod.Name);


Use MethodBase.GetCurrentMethod:

Returns a MethodBase object representing the currently executing method.

The MethodBase type has a Name property that is the name of the currently executing method as a string.

As a side note, perhaps you should look into existing logging frameworks so that you don't have to reinvent the wheel.

I would advise against doing this.

  • A logging method should generally have the minimum possible overhead, so that you can call it from performance-sensitive code.

  • Inlining means that you may not get the correct method, especially in release builds.

  • When logging exceptions, you will have the stack trace as part of the exception, which will give you the call stack at the moment the exception was thrown. When logging normal flow, it's much less useful to know the method name.

Need Your Help

Google App Engine with Python: Unable to update the data entity

python django google-app-engine

I have read, read, and read again the documentation and do many search on the Web but I don't understand yet why my app doesn't work correctly.

Safari for Windows - disable certain plug-ins

windows plugins safari

I just can't find a way of doing this - in Safari for Windows (5.1.7) I would like to disable certain plug-ins, but I can only see how to disable them all (from Preferences/Security-> Enable Plugin...