MSDN says this about the StackTrace
property of the Exception
class:
The StackTrace property holds a stack trace, which yo
You have to build the project with pdb files enabled and make sure your deploy the pdb files with your application. You can check if pdb files are in fact being built for you configuration by right clicking on the assembly you require pdb files for, then heading to Properties > Build > Advanced and making sure that under Output Debug Info is set to full.
To get the line numbers in the StackTrace, you need to have the correct debug information (PDB files) alongside your dlls/exes. To generate the the debug information, set the option in Project Properties -> Build -> Advanced -> Debug Info
:
Setting it to full
should suffice (see the Advanced Build Settings Dialog Box docs for what the other options do). Debug info (ie. PDB files) are generated for Debug build configurations by default, but can also be generated for Release build configurations.
Generating PDBs for release builds enables you to ship you code without the PDBs, but to drop the PDBs next to the dlls if you need line numbers (or even to attach a remote debugger). One thing to note is that in a release build, the line numbers may not be entirely correct due to optimisations made by the compiler or the JIT compiler (this is especially so if the line numbers show as 0).
Seem to have found the solution. On VS2010 at least, with Output Debug Info set to full I also did not get line numbers within the exceptions. The trick it seems was to turn on line numbers within the editor. (Tools -> Options -> Text Editor -> All Languages -> General -> Display -> Line numbers)
Now exceptions show up with line numbers.
Further to the other great suggestions, we were deploying to IIS. We had a staging server and a production server. They appeared identical except staging gave us line numbers and production did not. It turned out that there was an extra DLL in the bin directory of production (it happened to be SqlServerSpatial.dll fwiw) and once it was moved to the system directory line numbers started appearing on production.
The lesson was to ensure that the bin directory of production matches the bin directory of development in every respect (except for XML files).
If you have a web application or web service project using VS2012 or later, changing the build settings will not work. Instead, you should follow the advice in this article:
Visual Studio 2012 Website Publish Not Copying .pdb files
Specifically, you should include the following setting in the
<YOUR_PROJECT>\Properties\PublishProfiles\*.pubxml
file(s) for your project:
<PropertyGroup>
<ExcludeGeneratedDebugSymbol>False</ExcludeGeneratedDebugSymbol>
</PropertyGroup>
You could try the following, given there is a pdb file for the assembly:
try
{
throw new Exception();
}
catch (Exception ex)
{
// Get line number from the stack trace's top frame for the exception with source file information
int linenumber = (new StackTrace(ex, true)).GetFrame(0).GetFileLineNumber();
}