Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine calling method and class name? [duplicate]

I'm currently developing a application logging library using the built in TraceListener. This library will be used in many projects and should offer a simple interface where I only have to care about WHAT is going to be written in into the log file, but not HOW.

By using the reflection namespace, I can figure out which application currently called a log function (retrieving execution assembly name), but I want also the name of the function and class that called the logging function.

Let's say I have:

public static void LogInfo(string vLogText) {    Trace.WriteLine(         MethodInfo.GetCurrentMethod().Name         + this.GetType().ToString() + vLogText);    } 

When I call from another project (class: TestClass, method: TestMethod)

Tracer.LogInfo("log this!") 

I expect to see in the log:

TestClass, TestMethod, log this! 

But instead I got

TracerClass, LogInfo, log this! 

How to get the parent method and class name?

like image 719
Neurodefekt Avatar asked Feb 13 '13 08:02

Neurodefekt


People also ask

How can I get the class name that calls my method?

You can use StackFrame of System. Diagnostics and MethodBase of System. Reflection . StackFrame(Int32, Boolean) initializes a new instance of the StackFrame class that corresponds to a frame above the current stack frame, optionally capturing source information.

How can I find the method that called the current method?

Get Calling Method using Reflection [C#]Create new instance of StackTrace and call method GetFrame(1). The parameter is index of method call in call stack. Index of the first (the nearest) method call is „1“, so it returns a StackFrame of the calling method (method which directly called the current method).

Can we give class name and method name as same in Java?

We can have a method name same as a class name in Java but it is not a good practice to do so. This concept can be clear through example rather than explanations. In the below example, a default constructor is called when an object is created and a method with the same name is called using obj. Main().

How do you call all methods in a class?

Write the callAll() method to actually call each method explicitly without reflection. This allows for flexibility in subclassing. It also allows you to deal with methods which have parameters. It also allows you to bypass methods which don't apply.


2 Answers

Try doing something like this:

var mth = new StackTrace().GetFrame(1).GetMethod(); var cls = mth.ReflectedType.Name; // where 1 illustrates how deep into the stack to reflect. 

There are more elegant solutions in C# 5.0 >, however if you are using older frameworks, the above will help.

like image 132
Jens Kloster Avatar answered Sep 21 '22 01:09

Jens Kloster


You may just record the full stack trace instead of just the calling method using Environment.StackTrace. This may help you if you want to use the same logging structure to log exceptions.

Refer to this msdn page for more information.

like image 32
daryal Avatar answered Sep 21 '22 01:09

daryal