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
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.
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()
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)
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.
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.