How to show line number and frame in catch exception?

I am trying to write the line number and frame to a text file, but I cannot get it to work. From what I read online as I wrote, this should work, but in fact it does not output line numbers, which makes my debugging quite difficult. Can someone help, possibly pointing out where my code might be wrong?

catch (Exception e)
        {                
            var st = new StackTrace(e, true);
            var frame = st.GetFrame(0);
            var line = frame.GetFileLineNumber();
            var sw = new System.IO.StreamWriter(filename, true);

            sw.WriteLine(
                DateTime.Now.ToString() + "\r\n" 
                + e.Message + "\r\n" 
                + e.InnerException + "\r\n"                    
                + e.Source + "\r\n"
                + frame + "\r\n" 
                + line);
            sw.Close();

        }

It displays some information, not line / frame numbers.

Here is an example of getting the result.

22/08/2016 08:34:24
Input string was not in a correct format.
StringToNumber at offset 12099653 in file:line:column <filename unknown>:0:0 0

Also note that the application runs in Debug not Release.

+4
source share
1 answer

Make sure your application is created in DEBUG mode. If it is in Release, then some information, such as line numbers, is not included in the exception texts.

, Debug ( = > Build = > Advanced = > )

, , pdb , , .exe/.dll. , .

+2

Source: https://habr.com/ru/post/1652013/


All Articles