Visual Studio作为宇宙第一的IDE,开发调试.net core app,无一能出其右,我们还需要去了解Visual Studio Code吗?答案是肯定。

  • 杀鸡焉用牛刀:就一个hello world的Console App,还需要打开Visual Studio吗?
  • 画地为牢:如果开发环境换成Ubuntu或Centos等任意一个Linux发行版,调试怎么办?
  • 兵贵神速:马上就要开始调试一个已有app,但是调试环境没有Visual Studio,安装是不是特别费时?甚至不支持Visual Studio,怎么办?

那么Visual Studio Code的小而美的优势就会体现出来。

1.安装Visual Studio Code

纯属混字数的,扔一个链接https://code.visualstudio.com/,下载去吧,

2.安装.net core sdk

同样是扔一个链接https://dotnet.microsoft.com/download,页面选项卡,有4种环境

  • Windows
  • Linux
  • MacOS
  • Docker

按需选择吧

3.编码

略过

4.调试配置

假设你已经编码完成,

F5开始调试->Select Envoriment->.net core

4.1 生成并修改launch.json

然后就会创建.vscode文件夹,且创建了launch.json

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
      
    {
      "name": ".NET Core Launch (web)",
      "type": "coreclr",
      "request": "launch",
      "preLaunchTask": "build",
      "program": "${workspaceFolder}/Vmware.Sphere.Api/bin/Debug/netcoreapp3.1/properties/netcoreapp3.1/Vmware.Sphere.Api.dll",
      "args": [],
      "cwd": "${workspaceFolder}/Vmware.Sphere.Api",
      "stopAtEntry": false,
      "serverReadyAction": {
        "action": "openExternally",
        "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
      },
      "env": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "sourceFileMap": {
        "/Views": "${workspaceFolder}/Views"
      }
    },
    {
      "name": ".NET Core Attach",
      "type": "coreclr",
      "request": "attach",
      "processId": "${command:pickProcess}"
    }
  ]
}
  • program:调试的dll路径
  • cwd:正在调试的程序的工作目录的路径。默认是当前工作区

其他属性,还想知道具体作用,鼠标悬停在launch.json的某一个属性上,就可以了解,并根据实际情况进行修改。

4.2 生成task.json

F5->Could not find the task ‘build’->Configure Task->Select a task to configure->Create task.json file from temple->.Net Core

就会继续在.vscode下创建task.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "shell",
            "args": [
                "build",
                // Ask dotnet build to generate full paths for file names.
                "/property:GenerateFullPaths=true",
                // Do not generate summary otherwise it leads to duplicate errors in Problems panel
                "/consoleloggerparameters:NoSummary"
            ],
            "group": "build",
            "presentation": {
                "reveal": "silent"
            },
            "problemMatcher": "$msCompile"
        }
    ]
}
  • preLaunchTask:“build”,Task to run before debug session starts.在启动调试会话之前运行的任务,说白了,就是调试之前,总得构建生成吧。然后就会触发task中lable名为build的构建任务
    • command+args=dotnet build …

5.调试快捷键

  • Continue / Pause F5
  • Step Over F10
  • Step Into F11
  • Step Out Shift+F11
  • Restart Ctrl+Shift+F5
  • Stop Shift+F5
  • Run (Start Without Debugging) action is triggered with Ctrl+F5

现在就可以正常调试了

  • 断点
  • 日志断点
  • 变量监控
  • 等等

参考链接

https://code.visualstudio.com/docs/editor/debugging#_launch-configurations