noo*_*ber 2 .net c# assert stack-frame
我写了一个方法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);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么触发时我的行和列都等于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);
}
}
Run Code Online (Sandbox Code Playgroud)
(顺便看一下你的评论,你将需要PDB文件.这就是存储调试信息的地方.说实话,这对我来说是否能在SQLCLR触发器中运行是不明白的.老实说.以上对我有用.一个控制台应用程序,但这就是我所能说的...)