Codementor Events

Cleaner stacktraces.

Published Feb 17, 2020
Cleaner stacktraces.

Here's a typical stack-trace:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Contoso.Calculator.Host.Configuration.ReadConfiguration() in  C:\Users\bob\repos\Contoso\Calculator\Host\Configuration.cs:line 14
   at Contoso.Calculator.Host.Program.Initialise() in C:\Users\bob\repos\Contoso\Calculator\Host\Program.cs:line 23
   at Contoso.Calculator.Host.Program.Main() in C:\Users\bob\repos\Contoso\Calculator\Host\Program.cs:line 5

I don't know about you, but I'm not a huge fan of leaking the build path of my software - it's verbose, and, if you build all your software in C:\Temp\Garbage like a former colleague did, unprofessional looking.

The good thing is, this is easily fixed using the PathMap property. Simply add the following to your Directory.Build.props file

<PropertyGroup Label="Normalise stack trace file locations">
  <PathMap>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)'))=X:\</PathMap>
</PropertyGroup>

Your stack trace will now look much neater

System.NullReferenceException: Object reference not set to an instance of an object.
   at Contoso.Calculator.Host.Configuration.ReadConfiguration() in X:\Calculator\Host\Configuration.cs:line 14
   at Contoso.Calculator.Host.Program.Initialise() in X:\Calculator\Host\Program.cs:line 23
   at Contoso.Calculator.Host.Program.Main() in X:\Host\Program.cs:line 5

Note that you might want to hide the full path when building on a build-server, but show it when building on your local machine. This is easily achieved by adding a condition to the PropertyGroup:

<PropertyGroup 
    Label="Normalise stack trace file locations" 
    Condition="$(BuildingInsideVisualStudio) != 'true'">
Discover and read more posts from Rowland Banks
get started
post commentsBe the first to share your opinion
nijikoy695
2 months ago

Cleaner stacktraces are essential for efficient debugging, streamlining the process by providing clearer insights into code execution. Embracing such methodologies ensures smoother operations for websites like https://adelaidepressurecleaningpros.com.au, optimizing user experiences and minimizing disruptions. With meticulous attention to detail, developers can enhance readability and pinpoint errors swiftly. Implementing best practices not only improves code quality but also boosts productivity. For instance, by utilizing advanced logging frameworks and error handling techniques, developers can generate cleaner stacktraces, facilitating smoother troubleshooting experiences.

Show more replies