Yanayaya
Yanayaya

Reputation: 2184

How do I show the line number and frame in a catch exception?

I'm trying to write the line number and frame to a text file but I cannot get it to work. From what I've read online the way I've written this should work but it's not actually outputting any line numbers making my debugging quite hard. Can anyone assist in perhaps pointing out where my code could 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 does output some information just not the line / frame numbers.

Here is an example of what's getting output.

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 please note the application is running in Debug not Release.

Upvotes: 6

Views: 1052

Answers (1)

dlxeon
dlxeon

Reputation: 2000

Make sure your application is built in DEBUG mode. If it is in Release, than some information like line numbers are not included in exception texts.

Another possible reason is that you edited Debug configuration for your project and disabled debug information (Project settings => Build => Advanced => Output debug info )

And finally you need pdb files for line numbers and they should be in same folders as your .exe / .dll. By default, they are there, until you manually remove them or copy parts of your application to another location and run there.

Upvotes: 4

Related Questions