Line Number in Exceptions

databoks
databoks used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Miguel OzSenior Software Engineer
Top Expert 2009

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

Check:
http://stackoverflow.com/questions/51768/print-stack-trace-information-from-c
http://stackoverflow.com/questions/628565/display-lines-number-in-stack-trace-for-net-assembly-in-release-mode
Top Expert 2015

Commented:
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.

Author

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

Will try comment 1.

Thanks anyway.
Top Expert 2015

Commented:
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.

Author

Commented:
No suitable solution.

Cannot publish my proejct as a debug version.
Top Expert 2015

Commented:
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.

Author

Commented:
Sorry wrong words.

I meant i Cant distribute this as a debuggable version.
Miguel OzSenior Software Engineer
Top Expert 2009

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

Author

Commented:
Will try.

Than kyou.
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.
Commented:
I have thought about this and i came to this conclusion:

What does the hardcore programmers when debugging there applications? they dont use line numbers! They use good Error Handling and code comments.

So after digging with this, and made a speciel Class just to handle errors and in that class i made some functions that writes the Exception messages and the caller of the object. This helped.

Author

Commented:
Thank you all guys.
Top Expert 2015

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial