Question

Okay; assuming this code running in debug mode -

static StackFrame GetTopFrameWithLineNumber(Exception e)
{
    StackTrace trace = new StackTrace(e);
    foreach (StackFrame frame in trace.GetFrames())
    {
        if (frame.GetFileLineNumber() != 0)
        {
            return frame;
        }
    }
    return null;
}

I'm ALWAYS returning null. Why do the stack frames have no line numbers when if I inspect the Exception.StackTrace string, it clearly does have them for any non-framework code? Is there some issue with constructing a stack trace from an exception that i'm not aware of?

EDIT FOR CLARITY: In the thrown exception I can see the line numbers in the StackTrace property. I'm assuming that means I have everything else I need.

Was it helpful?

Solution

According to the documentation on the StackTrace constructor overload that takes an exception you can't expect line numbers when creating the StackTrace that way.

The StackTrace is created with the caller's current thread, and does not contain file name, line number, or column information.

To get the line numbers, you need to use the overload that takes a bool as well as the exception.

You also need symbol files (pdb) for line numbers. Symbol files are available for both debug and release builds.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top