Display lines number in Stack Trace for .NET assembly in Release mode
- 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.
Check the DEBUG constant parameter. - 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
Add line numbers to stack trace of ASP.NET web site that is deployed in release mode
In the build settings, set your project to generate debug symbols (pdb:s) in release mode too. If they are generated they will be automatically included on deploy. There is no need to run the entire site in debug mode, you just need the debug symbols available.
The setting is in the properties of the project. Select the build configuration you are using when publishing. Then on the Build tab, push the Advanced...
button and set debug info to "Full".
Exception line numbers without deploying PDBs in Release mode
Not without extensively modifying your code. Remember that the PDB
file has the necessary details to make your source code to the compiled output. Without out this, you will need to employ some sort of tracing to be able correlate the exception to the line number in your code. This can be achieved with techniques like specifying details in the exception message or using error codes.
The problem with these techniques and your goal is you will need to catch exceptions from code that isn't yours and wrap it in another exception. This is considered to be a poor practice as it is way too easy to swallow exceptions or incorrectly throw the wrapped exception causing you to loose your stack trace.
Take a look at the Stack Overflow article Release generating .pdb files, why? for more information on this matter.
StackTrace return wrong line number of exception in release mode
Why?
Because in release mode, the optimizer can inline functions and do other things that change the actual line number from what you have in source code.
Just outputting the stack trace is probably not helpful (IMHO). What type of exception did you get? What was the error message? What method did it originate from? Those are more helpful for diagnosing problems in my experience. I would change you exception handling to at least Console.WriteLine(ex)
. That will give you all of that data plus the stack trace.
This link can be helpful.
Get full stack trace with line numbers
I suspect you just want to call the overload taking a bool
:
var stackTrace = new StackTrace(true);
From the documentation:
Parameters
fNeedFileInfo
Type:System.Boolean
true
to capture the file name, line number, and column number; otherwise,false
.
Access the StackTrace when deploying in Release mode
You would need to deploy your .pdb
symbol files in order to do that in Release mode (and possibly turn off some optimisations, which may or may not be acceptable).
Related SO questions:
Display lines number in Stack Trace for .NET assembly in Release mode
How to generate PDB’s for .net managed projects in release mode? (cheers, Nick)
Print stack trace information from C#
Is showing the Exception StackTrace useful in a RELEASE assembly or only a DEBUG .dll
Related Topics
When Should I Use a List VS a Linkedlist
Tips For Optimizing C#/.Net Programs
Getting Multiple Keys of Specified Value of a Generic Dictionary
When Should I Use Double Instead of Decimal
Split a String by Another String in C#
What Does the @ Symbol Before a Variable Name Mean in C#
Comparing Object Properties in C#
How to Make a Winforms App Go Full Screen
How to Associate a File Extension to the Current Executable in C#
How to Create Linq Expression Tree to Select an Anonymous Type
Get String Between Two Strings in a String
How to Change Row Color in Datagridview