开发者

.net StackFrame and current line/column

I wrote a method 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.S开发者_开发问答tackFrame(1)).GetFileColumnNumber() + "\r\n" +
                "Where:" + (new System.Diagnostics.StackFrame(1)).GetMethod().Name;
            Log("ASSERTION", message);
        }
    }

Why do I have both line and column being equal to 0, when triggered? It supposed to be the place where Debug.Assert(false) is called.

Regards,


You need to use the StackFrame(int, bool) overload and specify true as the second argument. It looks like just the StackFrame(int) overload doesn't capture source information.

Sample code:

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);
    }
}

(Looking at your comments by the way, you will need the PDB files. That's where the debug information is stored. It's not at all clear to me whether this will work in a SQLCLR trigger, to be honest. The above works for me in a console app, but that's all I can say...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜