Debugging and Diagnostics

45.1 Visual Studio Debugger

Shortcut / Usage F9 (conditional, hit count, actions/tracepoints via right-click) Step Over / Into / Out F10 / F11 / Shift+F11 Locals / Autos / Watch windows Evaluate expressions: ? customer.Name See the execution path Edit and Continue Change code while paused

Break when an exception is thrown (Ctrl+Alt+E) Hover over variables CPU/memory while debugging

45.2 Code-Level Diagnostics

Debug.WriteLine($"Value: {x}")                 ' only in Debug builds
Debug.Assert(x > 0, "x must be positive")
Trace.WriteLine("Also in Release builds")
Stop                                           ' VB statement: breaks into the debugger (like Debugger.Break())

#If DEBUG Then
    Console.WriteLine("Debug build")
#End If
#Const FEATURE_X = True
#If FEATURE_X Then
#End If

<Conditional("DEBUG")>
Sub DumpState()
End Sub

<DebuggerDisplay("{Name} ({Age})")>
Public Class Person
End Class

45.3 Logging

' Microsoft.Extensions.Logging (with Serilog/NLog providers)
_logger.LogInformation("Order {OrderId} placed by {User}", orderId, user)
_logger.LogError(ex, "Failed to process order {OrderId}", orderId)
' Simple apps: My.Application.Log, Trace listeners, or NLog/Serilog directly

45.4 Performance and Memory Diagnostics

Visual Studio Profiler (CPU usage, memory usage, .NET object allocation), Stopwatch timing, dotnet-counters, dotnet-trace, dotnet-dump, PerfView, BenchmarkDotNet (works with VB).