Link to home
Create AccountLog in
Avatar of databoks
databoksFlag 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
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

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

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.
Avatar of databoks


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

Will try comment 1.

Thanks anyway.
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.
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.
Sorry wrong words.

I meant i Cant distribute this as a debuggable version.
My previous comment does require the pdb only not a full debuggable version. Please give it a go
Will try.

Than kyou.
Avatar of multithreading

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.
Avatar of databoks
Flag of Denmark image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thank you all guys.
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.