Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Logging 为什么使用呼叫者信息属性_Logging_.net 4.5_Stack Trace_C# 5.0 - Fatal编程技术网

Logging 为什么使用呼叫者信息属性

Logging 为什么使用呼叫者信息属性,logging,.net-4.5,stack-trace,c#-5.0,Logging,.net 4.5,Stack Trace,C# 5.0,在堆栈跟踪对象上使用调用方信息属性是否更好?有什么区别 考虑以下使用调用方信息属性进行日志记录的代码 public void Log( string message, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0) { // Do logging. } 从

在堆栈跟踪对象上使用调用方信息属性是否更好?有什么区别

考虑以下使用调用方信息属性进行日志记录的代码

public void Log(
    string message,
    [CallerMemberName] string memberName = null,
    [CallerFilePath] string filePath = null,
    [CallerLineNumber] int lineNumber = 0)
{
    // Do logging.
}
从我的测试中,使用
StackTrace
类可以获得相同的结果

public void Log(string message)
{
    StackTrace trace = new StackTrace(true);
    StackFrame frame = trace.GetFrame(1);

    string filePath = frame.GetFileName();
    int lineNumber = frame.GetFileLineNumber();
    string memberName = frame.GetMethod().Name;

    // Do logging.
}
我所看到的唯一区别是匿名方法,其中
StackTrace
包括调用方法的生成名称,如
b_a


我不喜欢caller info属性的一点是,尽管它们不必作为参数传入,但它们必须在每个要使用它们的方法上定义为方法参数,这违反了DRY原则。

这正是他们添加它的原因。因为使用StackTrace类既昂贵又不可靠。它怎么不可靠?