.net StackFrame和当前行/列
本文关键字:StackFrame net | 更新日期: 2023-09-27 18:08:10
我写了一个方法Assert():
[System.Diagnostics.Conditional("DEBUG")]
internal static void Assert(bool condition)
{
if (!condition)
{
var message =
"Line:" + (new System.Diagnostics.StackFrame(1)).GetFileLineNumber() + "'r'n" +
"Column:" + (new System.Diagnostics.StackFrame(1)).GetFileColumnNumber() + "'r'n" +
"Where:" + (new System.Diagnostics.StackFrame(1)).GetMethod().Name;
Log("ASSERTION", message);
}
}
为什么触发时,行和列都等于0 ?这里应该是调用Debug.Assert(false)的地方。
认为,
您需要使用StackFrame(int, bool)
过载并指定true
作为第二个参数。看起来只是StackFrame(int)
过载不能捕获源信息。
示例代码:
using System.Diagnostics;
...
[Conditional("DEBUG")]
internal static void Assert(bool condition)
{
if (!condition)
{
StackFrame frame = new StackFrame(1, true);
var message = string.Format("Line: {0}'r'nColumn: {1}'r'nWhere:{2}",
frame.GetFileLineNumber(),
frame.GetFileColumnNumber(),
frame.GetMethod().Name);
Log("ASSERTION", message);
}
}
(顺便看看您的注释,您将需要PDB文件。这就是存储调试信息的地方。老实说,我根本不清楚这是否会在SQLCLR触发器中起作用。以上方法适用于主机应用,但我只能说这么多了。)