2

我写了一个方法 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) 的地方。

问候,

4

1 回答 1

8

您需要使用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 触发器。以上内容适用于我一个控制台应用程序,但我只能这么说......)

于 2011-09-11T21:33:41.467 回答