39

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 Debug / Release builds??

Jack Kada
  • 24,474
  • 29
  • 82
  • 106
  • Yes, without the debug information the CLR cannot report the line number. It still shouldn't be too difficult to find the method and look at the issue in a debug session. – Lazarus Feb 03 '10 at 12:56
  • dupe: http://stackoverflow.com/questions/1328836 – raven Feb 12 '10 at 15:10
  • possible duplicate of [Show line number in exception handling](http://stackoverflow.com/questions/688336/show-line-number-in-exception-handling) – bmargulies Oct 25 '12 at 14:52
  • @Lazarus : ... unless this is on the QA machine. I don't understand why it doesn't just display the IL or something of the offending line. Then I could Reflector it back to C# and find it that way. This way its like "Cannot get ye flask". – micahhoover Aug 29 '13 at 14:03
  • This could be a problem if you're using async await also. Please provide code sample. – niico Oct 31 '19 at 17:46

6 Answers6

44

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)

Beetee
  • 475
  • 1
  • 7
  • 18
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
23

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.

Justin
  • 84,773
  • 49
  • 224
  • 367
15

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()
Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
4

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.

CARLOS LOTH
  • 4,675
  • 3
  • 38
  • 44
1

For .NET Core, .NET 5 and later, to have full exception line numbers in release builds, configure the project as follows:

<PropertyGroup>    
  <DebugSymbols>true</DebugSymbols>
  <DebugType>embedded</DebugType>

    <!-- Only enable the following if the line numbers mismatch -->
    <!--<Optimize>false</Optimize>-->
    
    <!--
      Additional properties which may impact how printed line numbers match the source code line numbers are listed here:
      https://learn.microsoft.com/en-us/dotnet/core/run-time-config/compilation
    -->
</PropertyGroup>

The above configuration will include debug symbols directly with the built files, which can be published as nugets.

An alternative to the above is to restore debug packages together with the main nuget packages, which is currently not yet supported: https://github.com/NuGet/Home/issues/9667

aleksander_si
  • 1,021
  • 10
  • 28
0

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.

pdschuller
  • 584
  • 6
  • 26