How to Debug C# (.Net Core) with VS Code
Summary
- Basic
- Spec
- install
- debugging unit test (XUnit)
- debugging Console Program
- debugging ASP.NET
- attach to local process
- attach to remote process
Basic
- Welcome to .NET Core!
- Extension: C#
- Debugger: .NET Core function
- module code: BubbleSort/BubbleSorter.cs
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
- install .net Core SDK
- install Extension
- When you open C# code, it’ll start to install necessary tools.
debugging unit test (XUnit)
- test code: BubbleSortTest/TestSort.cs
Inline
- open xunit code.
- click Yes to message “Required assets to build and debug are missin from …
- show Inline
debugging Console Program
- Console Program code: BubbleSorter/Program.cs
way need to open project dir
- change VS Code dir to the project dir.
- open C# code in the project.
- click Yes to message “Required assets to build and debug are missing from …
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
- WebAPI source: BubbleSorterAPI/Controller/BubbleSortController.cs
- WebClient source: BubbleSorterAPI/wwwroot/index.html
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
- launch .NET Core app
cd BubbleSorterAPI
dotnet run
- start a debug
- 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
- 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
- start debug
- select process