Code Monkey home page Code Monkey logo

csharp's Introduction

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", "[email protected]"],
        // set remote vsdbg path
        "debuggerPath": "~/vsdbg/vsdbg"
      }
    }
  ]
}

how-to

  1. start .NET App at remote host
# access remote host
ssh [email protected]
# run .NET App
cd vscode-debug-specs/csharp/BubbleSorterAPI
dotnet run
  1. start debug
  2. select process

csharp's People

Contributors

74th avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.