Advanced Debugging

Steve Temple

A little about me:

Steve Temple

Developer for over 25 years

Technical Director & co-founder of Gibe

Umbraco MVP x 3

Co-organiser of UmbracoSpark

Admin of Umbraco Community Mastodon

Mastodon

https://umbracocommunity.social

Advanced Debugging.

Debugging is not often taught, something you pick up on the job.

Description:  Nobody comes out of university with knowledge of Visual Studio's debugging features. Visual Studio has one of the most seamless debugging experiences for web development, but it's something you pick up on the job, not something you're taught. I'm going to demonstrate some of the more useful debugging features inside Visual Studio 2022.

Exception Details: System.Debugging.LackOfExperienceException: Gain more debugging experience to improve your development speed and capabilities.

Source Error:

[LackOfExperienceException (0x80008404): Gain more debugging experience to improve your development speed and capabilities]
   System.Debugging.Learn(ISkill skill, int visualStudioVersion) +124
   System.Debugging.GetStarted() +279
   System.Debugging.Talk(bool advanced) +56
   Umbraco.Codegarden.Theatre.Init() +10218
   Umbraco.Codegarden.Start()


Version Information: DF23 v2023.9.28

Basic Debugging.

Only run in debug mode when you want to debug, not all the time.

Description:  
If you're using IIS Express, Clicking start (F5) will by default run in debug mode. Debug mode is slow, don't run in debug mode unless you need to debug.

Use Start without debugging (Ctrl+F5) when running an application, you can always attach to process after if you want to start.

Exception Details: System.Debugging.UneccessarilySlowException: Running in debug mode when you aren't debugging will slow your development times down

Source Error:

[UneccessarilySlowException (0x80008400): Running in debug mode when you aren't debugging will slow your development times down]

*------------------------------------------------*
| Spin up times of an Umbraco v11 site:          |
| -----------------------------------------------|
| Debug mode:            | 33s                   |
| Non-debug mode:        | 17s                   |
| -----------------------------------------------|
| Average page load time:                        |
| -----------------------------------------------|
| Debug mode:            | 43ms                  |
| Non-debug mode:        | 24ms                  |
*------------------------------------------------*

 

Version Information: DF23 v2023.9.28

Basic Debugging.

Interpreting error messages.

Description:  An incredibly useful debugging skill is to be able to interpret the error messages shown.
We've all seen these errors and they'll give you useful clues where to look.
Everyone has seen a NullReferenceException: Object reference not set to an instance of an object
Or how about 0x80004005, which is almost always some sort of permission issue


Exception Details: System.Debugging.RTFSTException: Read the flipping stack trace exception. Most errors you'll encounter can be diagnosed if you learn how to interpret the Stack Trace

Source Error:

[RTFSTException(0x80001400): Read the flipping stack trace exception.]
   System.Debugging.Comprehension.NotFound() +417
   System.Debugging.StackTrace.Read(StackTrace stackTrace) +279
   System.Web.Error() +56
   System.Web.UI.Page.DeterminePostBackMode() +111
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint) +10218

Version Information: DF23 v2023.9.28

Debugging.

Let's debug a site

Description:  Let's have a crack at running a site in debug mode, setting breakpoints and stepping through the code and see if we can fix some of the issues.

Source Error:

[LiveDemoException(0x80004005): The odds of a live demo working seamlessly are pretty low.]
   System.Speaker.WhyAreYouMakingLifeHardForYourself() +347
   System.Conference.Wifi() +417
   System.InternetRequired() +279


Version Information: DF23 v2023.9.28

Debugging.

Improving your debugging experience.

Description:  There are some tools inside Visual Studio that make your debugging experience nicer.
We've already covered watches, but we can use DebuggerDisplayAttribute to make the display in that cleaner
We can also use Tracepoints to output debug display as we run the code

 

Source Error:

[DebuggerDisplay("Tweet by {Username} - {Text}")]
public class Tweet
{
  public string Text { get; set; }
  public string Username { get; set; }
}

Sample Tracepoint:
$FUNCTION Found tweet by {usernames[0]} {comments[i]}

Version Information: DF23 v2023.9.28

Debugging - Did you know?

The immediate window can be a great tool to help with your debugging

Description:  The immediate window can help you by changing variables in your code during debug

You can use ", nse" to use the immediate window but have no side effects i.e. no actual changes to the variable after the immediate effect has run
You can use the immediate window to debug your code while you're writing it at design time

Source Error:

[ImmediateWindowException(0x40018702): The immediate window is more powerful than you think 
    ChangeVariablesDuringDebug() +12
    DebugAtDesignTime() +21
    SideEffectFreeFunctionEvaluation(", nse") +564

Version Information: DF23 v2023.9.28

Advanced Debugging.

Remote debugging Azure App Services

Description: Azure App Services has an option to remote debug the site, you can connect to a remote site and debug it straight from visual studio using the live traffic.

This does have some limitations, if you hit a breakpoint requests are paused, on production. Let's do some conditional breakpoints so it's only our requests that cause a breakpoint.

Getting this working can be a bit tricky if you're using a build pipeline as it will want the pdb to be an exact match to the dll.

You can get around this by downloading the .pdb file from the app service using the portal.


Exception Details: System.Debugging.RemoteDebuggingException: Multiple steps are involved and it can cause performance issues on the remote server if you're debugging directly on production.

Source Error:

ConditionalBreakpointSample:
    HttpContext.Request.QueryString.Value == "?debug"

Version Information: DF23 v2023.9.28

Advanced Debugging.

Is that Azure App Services only?

Description: No it's not, there is a remote debugger you can install on VMs, servers, etc if you have permission to access the servers via RDP etc.

You'll need to allow access to a removed debugging port on the server, I'd recommend restricting that by IP or via a VPN etc.

Some hosting companies might provider that out of the box

I'm going to talk to Umbraco about enabling this on Umbraco Cloud sites


Exception Details: System.Debugging.RemoteDebuggingException: Multiple steps are involved and it can cause performance issues on the remote server if you're debugging directly on production.

Source Error:

https://learn.microsoft.com/en-us/visualstudio/debugger/remote-debugging?view=vs-2022

Version Information: DF23 v2023.9.28

Advanced Debugging.

Debugging snapshots

Description: Azure Application Insights can be configured to record Exceptions that occur on production App Services as snapshots 

You need to have Application Insights setup and install and configure the Microsoft.ApplicationInsights.SnapshotCollector nuget package.

A sample of the exceptions will then be recorded in ApplicationInsights and you can download them and view the state of the application when an error occurred, allowing you to view variables etc.


Exception Details: System.Debugging.SnapshotDebuggerException: Quickly diagnose production exceptions with live data

Source Error:

[SnapshotDebuggerException(0x80080085): Quickly diagnose production exceptions with live data.]
   System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection) +417
   System.Web.HttpRequest.ValidateHttpValueCollection(HttpValueCollection collection, RequestValidationSource requestCollection) +279
   System.Web.HttpRequest.get_QueryString() +56
   System.Web.UI.Page.DeterminePostBackMode() +111
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +10218

Version Information: DF23 v2023.9.28

Advanced Debugging.

Debugging snapshots

Description: You can do this wherever you can run Application Insights, so can be a VM, internal server, App Services, AWS VM etc

Has some advantages over remote debugging as that you don't need to be able to reproduce the error locally; It won't interrupt your live traffic or slow your server down. 

Also has some disadvantages, you are looking at a history snapshot, you can't run the code


Exception Details: System.Debugging.SnapshotDebuggerException: Quickly diagnose production exceptions with live data

Source Error:

[SnapshotDebuggerException(0x80080085): Quickly diagnose production exceptions with live data.]
   System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection) +417
   System.Web.HttpRequest.ValidateHttpValueCollection(HttpValueCollection collection, RequestValidationSource requestCollection) +279
   System.Web.HttpRequest.get_QueryString() +56
   System.Web.UI.Page.DeterminePostBackMode() +111
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +10218

Version Information: DF23 v2023.9.28

Debugging - Did you know?

You can step backwards when debugging

Description: Enabling IntelliTrace snapshots allows you to step backwards through your code and see the code that run before your breakpoint.

In VisualStudio -> Debug -> IntelliTrace -> Open IntelliTrace Settings, select IntelliTrace Snapshots

Exception Details: HaveYouTriedException: Maybe some of these tools will make your life easier

Source Error:

[HaveYouTriedException(0x40005021): Maybe some of these tools will make your life easier  
    FoundIt() +457
    StepBackwards() +236
    InsertBreakpoint() +134
    ThereIsABug() +666

 

Version Information: DF23 v2023.9.28

Thank you

Made with Slides.com