堆栈跟踪行号错误,debug = false和compilerOptions =“/ debug:pdbonly”

我正在使用asp.net 3.5

在web.config中,我有debug = false和compilerOptions =“/ debug:pdbonly”来进行优化编译,并且仍然在我的堆栈跟踪中获取行号。 这在大多数情况下都有效,但我的App_Code文件夹中的函数中有一个错误,堆栈跟踪表示错误在一行上可能不是错误。

我玩了一下web.config设置,发现如果我设置了debug = true和compilerOptions = pdbonly,那么堆栈跟踪会说错误是直接在错误行之后的行。 如果我删除compilerOptions = pdbonly,则堆栈跟踪会将正确的行报告为错误。

//the actual bug (only reported when debug=true and no compiler options set) var dt = new DateTime(-1,-1,-1); // //...lots of non-buggy code between // //the bug according to the stack trace when // debug=false and compilerOptions="/debug:pdbonly" var ts = TimeSpan.Empty; 

有没有办法使这项工作正确?

编译器优化可能会影响堆栈跟踪中报告的行编号。 您可以设置编译器选项/ optimze-来禁用它们,然后您的堆栈跟踪行号应该是正确的。

例如: