/csharp

How to Debug csharp with VS Code

Primary LanguageC#Creative Commons Zero v1.0 UniversalCC0-1.0

How to Debug C# (.Net Core) with VS Code

Summary

Basic

Spec

  • OS

    • MacOS

    • Windows

    • Linux

  • Break Point _ break point _ condition break point * function breakpoint

  • Step Execution _ Step Over _ Step Into _ Step Out _ Continue

  • Variables _ variables views _ watch variables

  • Call Stack * call stack

  • Evaluation _ eval expression to show variables _ eval expression to change variables

  • Type of Execution _ debug unit test _ debug executable package    * remote debugging

    • ASP.NET Core

install

debugging unit test (XUnit)

Inline

  1. open xunit code.
  2. click Yes to message "Required assets to build and debug are missin from ...

startup

  1. show Inline

XUnit

debugging Console Program

way need to open project dir

  1. change VS Code dir to the project dir.
  2. open C# code in the project.
  3. click Yes to message "Required assets to build and debug are missing from ...

startup

way no need to open project dir

add tasks.json to build task

tasks.json

{
  "version": "0.1.0",
  "command": "dotnet",
  "isShellCommand": true,
  "args": [],
  "tasks": [
    {
      // if you need multiple tasks, change taskName
      "taskName": "build",
      "args": ["${workspaceRoot}/BubbleSorter/BubbleSorter.csproj"],
      "isBuildCommand": true,
      "problemMatcher": "$msCompile"
    }
  ]
}

add the debug setting to launch.json (Add Configuration Menu: '.NET: launch .NET Core Console App').

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": ".NET Core Launch (console)",
      "type": "coreclr",
      "request": "launch",
      // set build task name
      "preLaunchTask": "build",
      // set dll path
      "program": "${workspaceRoot}/BubbleSorter/bin/Debug/netcoreapp2.0/BubbleSorter.dll",
      "args": ["4", "3", "2", "1"],
      // set project dir path
      "cwd": "${workspaceRoot}/BubbleSorter",
      "console": "internalConsole",
      "stopAtEntry": false,
      "internalConsoleOptions": "openOnSessionStart"
    }
  ]
}

debugging ASP.NET

add the debug setting to launch.json (Add Configuration Menu: '.NET: launch a .NET Core Web App').

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": ".NET Core Launch (web)",
      "type": "coreclr",
      "request": "launch",
      "preLaunchTask": "build",
      "program": "${workspaceRoot}/BubbleSorterAPI/bin/Debug/netcoreapp2.1/BubbleSorterAPI.dll",
      "args": [],
      "cwd": "${workspaceRoot}/BubbleSorterAPI",
      "stopAtEntry": false,
      "launchBrowser": {
        "enabled": true,
        "args": "${auto-detect-url}",
        "windows": {
          "command": "cmd.exe",
          "args": "/C start ${auto-detect-url}"
        },
        "osx": {
          "command": "open"
        },
        "linux": {
          "command": "xdg-open"
        }
      },
      "env": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  ]
}

access http://localhost:5000/index.html and debug.

attach to local process

add settings to launch.json.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": ".NET Core Attach",
      "type": "coreclr",
      "request": "attach",
      "processId": "${command:pickProcess}"
    }
  ]
}

how-to

  1. launch .NET Core app
cd BubbleSorterAPI
dotnet run
  1. start a debug
  2. select the process

attach to remote process

install vsdbg to remote host (following script install to ~/vsdbg).

curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg

add settings to launch.json.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": ".NET Core remote Attach",
      "type": "coreclr",
      "request": "attach",
      "processId": "${command:pickRemoteProcess}",
      "sourceFileMap": {
        // create a map between remote and local directory
        // "remote host directory" : "VS Code(local) directory"
        "/home/nnyn/vscode-debug-specs/csharp": "/Users/nnyn/Documents/vscode-debug-specs/csharp"
      },
      "pipeTransport": {
        "pipeCwd": "${workspaceRoot}",
        "pipeProgram": "ssh",
        // set remote host and ssh setting
        "pipeArgs": ["-T", "nnyn@192.168.64.6"],
        // set remote vsdbg path
        "debuggerPath": "~/vsdbg/vsdbg"
      }
    }
  ]
}

how-to

  1. start .NET App at remote host
# access remote host
ssh nnyn@192.168.64.6
# run .NET App
cd vscode-debug-specs/csharp/BubbleSorterAPI
dotnet run
  1. start debug
  2. select process