Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception stack trace does not have line numbers

Tags:

c#

.net

.net-core

I discovered a strange issue on NetCore that doesn't add the line numbers when you run the application on the server the stacktrace doesn't have line numbers. I create a console application using dotnet new console, added this code:

using System;

namespace bar2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                throw new InvalidOperationException("some error");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

later I run the app using dotnet run

the code above will print:

System.InvalidOperationException: some error
at bar2.Program.Main(String[] args)

notice that there is no line number if you execute it on linux (the pdb file are available in the folder).

How can I fix this? For production app is really hard to replicate the bug because each time I didn't know the file line number, I see only the error...

NetCore Info .NET Command Line Tools (2.1.200)

Product Information:
 Version:            2.1.200
 Commit SHA-1 hash:  2edba8d7f1

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  16.04
 OS Platform: Linux
 RID:         ubuntu.16.04-x64
 Base Path:   /usr/share/dotnet/sdk/2.1.200/

Host (useful for support):
  Version: 2.1.1
  Commit:  6985b9f684

.NET Core SDKs installed:
  2.1.200 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.0.7 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.1 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

I tried also debug and release mode with "pdb only" setted.

like image 322
jidic Avatar asked Jun 25 '18 07:06

jidic


People also ask

How can I get line number in stack trace?

The java. lang. StackTraceElement. getLineNumber() method returns the line number of the source line containing the execution point represented by this stack trace element.

How can I get the line number which threw exception?

Simple way, use the Exception. ToString() function, it will return the line after the exception description. You can also check the program debug database as it contains debug info/logs about the whole application.

What is stack trace in exception?

A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown.

What does the stack trace contain?

The stack trace contains the Exception's type and a message, and a list of all the method calls which were in progress when it was thrown.


1 Answers

  • Go into the Properties window for the project where you want to see stack trace line numbers.

  • Click on the Build "vertical tab".

  • Select "Release" configuration.

  • Uncheck the "Optimize code" parameter to avoid the occasional trace issue with inlined code (this step is not essential).
  • Press the Advanced... button and choose Output -> Debug Info -> pdb-only.
  • Deploy the generated .pdb file with the assembly.

Implemented with the comment below:

  • One other thing to check is in the "Package/Publish Web" section that the "Exclude generated debug symbols" checkbox is also unchecked

For more information look at below link:

Display lines number in Stack Trace for .NET assembly in Release mode

like image 196
Ali Tooshmalani Avatar answered Oct 01 '22 05:10

Ali Tooshmalani