C# Exceptions Not Giving Line Numbers

后端 未结 5 1979
攒了一身酷
攒了一身酷 2020-12-03 09:55

I am using C# having come from a Java background - I have an exception but it does not tell me the line number - Just the method name.

Is that usual?? Is it down to

相关标签:
5条回答
  • 2020-12-03 10:06

    Make sure the PDB files of your application are deployed with the application. The PDB files are the files that is used by the CLR to determine the source code line numbers.

    0 讨论(0)
  • 2020-12-03 10:11

    The StackTrace property of the Exception class contains line numbers, at least if the debug information (pdb file) is available:

    using System;
    class Program {
        public static void Main() {
            try {
                throw new Exception("test");
            } catch (Exception e) {
                Console.WriteLine(e.StackTrace);
            }
        }
    }
    

    will give the following output with the pdb file:

    at Program.Main() in X:\code\test\test\Program.cs:line 6
    

    and this without:

    at Program.Main()
    
    0 讨论(0)
  • 2020-12-03 10:17

    Line numbers should be available when compiling in Debug as long as you keep the pdb files in the application directory.

    This will also work in Release if you enable PDB creation, as per Justin's answer.

    If you are interested, more information can be found at PDB Files (MSDN)

    0 讨论(0)
  • 2020-12-03 10:26

    My app is a ClickOnce app (WPF) and I had to do something a little different:

    Project Properties designer > Publish > Application Files

    check the Show All Files checkbox

    find the pdb file and set its dropdowns there to include it

    I had Pdb-only set at: Build > Advanced > Debug Info

    Not sure if that matters.

    0 讨论(0)
  • 2020-12-03 10:32

    This is down to debug symbols not being available - when you build your project make sure that you have "full" set in the debug settings (Project properties -> Build -> Advanced -> Debug info) and also make sure that the resulting pdb files are present alongside the assmebly in the same directory when you are running your app.

    0 讨论(0)
提交回复
热议问题