是否可以在 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/