Avatar of databoks
Flag for Denmark asked on

Line Number in Exceptions

Hi Experts.

Is there any way i can include the line number in the StackTrace or Exception i a C# Applications.

The error is writting in a Log file and i want to include line numbers to make debugging eaiser
.NET ProgrammingC#

Avatar of undefined
Last Comment
Jacques Bourgeois (James Burger)

8/22/2022 - Mon
Miguel Oz

Use the stack trace class:
var trace = new System.Diagnostics.StackTrace(exception);
also you need to include the pdb file

Jacques Bourgeois (James Burger)

You need to compile the application with debug information, a .pdb file that you then need to distribute with the project.

To generate the .pdb, go into the Build tab of the project properties.
Select the proper configuration at the top.
Click on the Advanced... button.
Make sure that Debug Info is set to pdb-only.
pdb-only is sufficient. Full can slow down the application a bit and will generate a bigger .pdb file.

Note that doing so will make your program easier to decompile for anybody who would want to see what you are doing.

I dó not want to distribute this As a debuggable version.

Will try comment 1.

Thanks anyway.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
Jacques Bourgeois (James Burger)

My answer was simply a complement to the first comment.
Line numbers are not compiled and thus not part of the executable. In a debuggable version, there is a link between the .exe (or .dll) and the .pdb. When an exception occurs in the executable, the framework is able to retrieve the line number from the .pdb.
You thus need a debuggable version in order to get the line numbers in the StackTrace.

No suitable solution.

Cannot publish my proejct as a debug version.
Jacques Bourgeois (James Burger)

You did not tell us you were using ClickOnce for deployment (that is how you publish, isn't it?).

I do not know (I would like to know however) how to publish a debug version. There is a problem publishing the .pdb and linking it with the application.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Sorry wrong words.

I meant i Cant distribute this as a debuggable version.
Miguel Oz

My previous comment does require the pdb only not a full debuggable version. Please give it a go

Will try.

Than kyou.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck

I hate to throw cold water on a workable solution to an intractable problem, but here goes anyway: You shouldn't do this.

If you compile this for Debug to solve your missing line number it also activates conditional compilation. Your end users can see debug asserts that are meant only for Debug builds.

It is unfortunate that the line-by-line ".line" directives are left out of the MSIL in Release builds, but this is by design. Part of the reason is because the compiler is free to mess with your line ordering and even optimise away code. The other reason is to save a little space in a release build.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Thank you all guys.
Jacques Bourgeois (James Burger)

Exception handling is naturally the best thing to do.

But you oversimplify.

But no matter how well you work on that side of things, unexpected errors do happen. They will need to be corrected and/or proper exception handling will have to be built around those. And in order to do that, you will need to know where the error occured. So, hardore programmers do need line numbers. They do not display them in the editor, but getting an error report that gives the line and activating Ctrl-G in the editor is the fastest and easiest way to start correctint a problem.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.