3

We have a "logger" class which have log method that get the log message, and wrote to the log the message and the calling method, first we did the following also to send to the log method new parameter Method.Base.GetCurrentMethod(). I found another way, to use Relection.MethodBase:

public void Log(string message)
{
   stackTrace = new StackTrace();
   string methodName = stackTrace.GetFrame(1).GetMethod().Name;
   ....
}

But I've a problem, that every calling for the log method, I forced to create new instance from StackTrace, and when I trying to create the instance in the constructor, I get that the method name is InvokeMethod.

We're using MEF in our project. Any ideas how to improve the code?

4

4 回答 4

6

如果您使用的是最新最好的 C# (5) 版本,它会通过调用站点属性内置到语言中。你会像这样使用它:

public void Log(string message, [CallerMemberName] string methodName = null)
{
}

您在不提供第二个参数的情况下调用该方法,C# 编译器将自动为您填充它。您可以使用以下 Call-Site 属性:CallerMemberNameCallerLineNumberCallerFilePath

于 2012-12-16T09:51:13.323 回答
3

我建议对“信息日志”(没有异常详细信息的日志)使用完整的堆栈跟踪,使用

StackTrace stackTrace = new StackTrace(true); 
string stackTraceString = stackTrace.ToString();

将为您提供带有源信息的完整堆栈跟踪。

但是当你有日志有异常实例时,例如catch语句中的日志,你可以使用堆栈跟踪和异常本身的内部异常信息,而不仅仅是调用方法。

StackTrace stackTrace = new StackTrace(ex, true); 
string stackTraceString = stackTrace.ToString();

虽然 (ex) 是您需要它的堆栈跟踪的例外。

* 编辑 *

或者您可以使用:

Environment.StackTrace,它是一个字符串属性!但是包含get_StackTrace的调用,你可以写一个简单的代码来删除这部分,但我认为你可以根据需要制作堆栈跟踪对象,这没什么大不了的。

于 2012-12-16T08:46:19.840 回答
1

在实施您的解决方案之前,您应该了解方法内联。

看看Scott Hanselman 的这篇文章,他尝试实现几乎相同的功能。更多信息在这里

于 2012-12-16T08:50:15.530 回答
0

您可以为此目的使用 PostSharp。在页面上查看有关如何改进代码的示例。

于 2012-12-16T09:51:48.863 回答