C# 跟踪(记录)两点之间的所有代码行

标签 c# visual-studio visual-studio-debugging

是否可以在 VS 2013 (C#) 中跟踪(到文件)我的应用程序的执行路径?我的意思是逐行,就好像我在断点后一直按 F11,手动写下行...结果日志可能很大,我知道,但无论如何?

当然我想要源代码行,而不是 MSIL。

最佳答案

是的,这是可能的,但是这需要访问源文件,如果你 真的需要源代码。

使用 CallerInformationAttributes

C# 4.5 要求:

int _beginIndex;

public void BeginTrace(
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
{ 
    _beginIndex = sourceLineNumber;
}

public void EndTrace(
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
{
    int index = 0;
    foreach(var line in File.ReadLines(sourceFilePath))
    {
       if(index++ > _beginIndex && index <sourceLineNumber)
           Console.WriteLine(line);
    }
}

然后像这样使用它:

  BeginTrace();

  Foo foo = new Foo();
  foo.DoSomeStuff();
  foo.Bar();

  EndTrace();

这个例子只适用于同一个文件中的 Begin 和 End,除非你传递一些对象,否则很难完成其他事情......

关于C# 跟踪(记录)两点之间的所有代码行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30714286/

相关文章:

c# - 如何更改 ListView 中的项目索引?

c# - 如何使用 wix 安装程序在自定义文件夹中安装应用程序,而不是 Program Files 文件夹

c# - 在 Resharper 中为对象和数组初始化程序获取正确的缩进

C# - 应用程序意外终止而不留下痕迹

c# - VS 2010 - 输出窗口 - 如何启用 "find message in code"按钮?

c# - 在 LINQ 结果上将 IEnumerable<T> 转换为 List<T>,性能损失巨大

c# - 如何在不知道使用 Linq to XML 嵌套多深的情况下查找元素?

C# 快速排除排序列表的方法

c# - 无法找到或创建描述的执行上下文 <IBCocoaTouchPlatformToolDescription : 0x7fa8bad9a6f0>

c# - 为什么 RuntimeMethodInfo.Invoke 没有出现在我的 Visual Studio 调试器调用堆栈中?