如何确定调用方法和类名? [重复]

时间:2022-10-10 16:45:30

This question already has an answer here:

这个问题在这里已有答案:

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.

我目前正在使用内置的TraceListener开发应用程序日志库。这个库将在许多项目中使用,并且应该提供一个简单的界面,我只需要关心将什么内容写入日志文件,而不是如何。

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)

当我从另一个项目调用时(类:TestClass,方法: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?

如何获取父方法和类名?

3 个解决方案

#1


30  

The newest C# and VS 2012 shipped with Caller Information (namely CallerFilePathAttribute, CallerLineNumberAttribute and CallerMemberNameAttribute), which will help you if you can only use it.

最新的C#和VS 2012附带了Caller Information(即CallerFilePathAttribute,CallerLineNumberAttribute和CallerMemberNameAttribute),如果您只能使用它,它将为您提供帮助。

If you can't, you'd have to refer to other answers.

如果你不能,你必须参考其他答案。

#2


22  

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.

C#5.0>中有更优雅的解决方案,但是如果您使用的是较旧的框架,以上内容将会有所帮助。

#3


2  

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.

您可以使用Environment.StackTrace记录完整的堆栈跟踪,而不仅仅是调用方法。如果要使用相同的日志记录结构来记录异常,这可能会有所帮助。

Refer to this msdn page for more information.

有关更多信息,请参阅此msdn页面。

#1


30  

The newest C# and VS 2012 shipped with Caller Information (namely CallerFilePathAttribute, CallerLineNumberAttribute and CallerMemberNameAttribute), which will help you if you can only use it.

最新的C#和VS 2012附带了Caller Information(即CallerFilePathAttribute,CallerLineNumberAttribute和CallerMemberNameAttribute),如果您只能使用它,它将为您提供帮助。

If you can't, you'd have to refer to other answers.

如果你不能,你必须参考其他答案。

#2


22  

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.

C#5.0>中有更优雅的解决方案,但是如果您使用的是较旧的框架,以上内容将会有所帮助。

#3


2  

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.

您可以使用Environment.StackTrace记录完整的堆栈跟踪,而不仅仅是调用方法。如果要使用相同的日志记录结构来记录异常,这可能会有所帮助。

Refer to this msdn page for more information.

有关更多信息,请参阅此msdn页面。