How to Include Line Numbers in a Stack Trace Without a Pdb

How can I include line numbers in a stack trace without a pdb?

You can't get a stack trace with line numbers directly from your application unless you bundle the PDB. However, if you have the PDB files for the same version of the app that you ship to your customers, and you don't mind some light scripting, then you can turn the .NET stack trace and IL offsets back into line numbers.

During your build process, use Mike Stall's pdb2xml converter, distributed as part of his excellent MDbg managed code debugger, and store them some place secure (e.g., source control). When you get a stack trace back from the client, you can query the IL offset from the XML data to determine the relevant line number. If your stack traces get submitted to a website, you can even automate the conversion, so that developers will already be getting fully detailed stack traces by the time the cases hit their inbox.

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.

How to show line numbers in stack trace if single file is produced

Unfortunately this seems like a known bug and it's not closed even in recent (net6.0 as I'm writing it) .net versions. Our team is suffering from this issue too.

Although making the single file publication with .pdb files attached helps, it's writing something like:

<DebugType>embedded</DebugType> 

in your csproj file, more on that here.

But it's not clear if it's should be a default behaviour

Print stack trace with line numbers

You need to pass true to the constructor to instruct it to capture file/line information.

Alternatively, use Environment.StackTrace, if you just need the textual representation of the stack trace.

Additionally, the matching .pdb file must be in the expected location (usually next to the binary).



Related Topics



Leave a reply



Submit