StackFrame.GetFileLineNumber() Returns 0 in UWP

vaishali chaudhary 1 Reputation point
2021-12-16T10:46:58.237+00:00

Hi Team,

I want to get filename and linenumber of exception in UWP App. Below is the code snippet I am using. FileName and LIneNumber are coming as null and 0. The pdb files are available in the Appx and build path. Even in debug mode, I dont get the filename and lineNumber. The same code works in my ConsoleApp but fails in UWP.

   static void Main(string[] args)
    {
        try
        {
            Convert.ToInt32("xyx");
        }
        catch (Exception ex)
        {
            LogError(ex);
        }
    }
    private static void LogError(Exception ex)
    {
        StackTrace st = new StackTrace(ex, true);
        StackFrame stackFrame = st.GetFrame(st.FrameCount-1);
        string fileName = stackFrame.GetFileName();
        string methodName = stackFrame.GetMethod().Name;
        int lineNumber = stackFrame.GetFileLineNumber();
    }
}
Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 32,731 Reputation points Microsoft Vendor
    2021-12-17T03:20:58.707+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Although the StackFrame Class is supported to use in UWP, for example, you could call it in a UWP app, but it looks like not all the features of the StackFrame Class are supported, like GetFileLineNumber.

    Generally, if we want to collect or log exception messages, it is recommended to use App Center in UWP apps.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.