| title | How to Debug C# (.Net Core) with VSCode |
|---|---|
| permalink | /csharp/ |
- Basic
- Spec
- install
- debugging unit test (XUnit)
- debugging Console Program
- debugging ASP.NET
- attach to local process
- attach to remote process
- Welcome to .NET Core!
- Extension: C#
- Debugger: .NET Core function
- module code: BubbleSort/BubbleSorter.cs
- 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 .net Core SDK
- install Extension
- When you open C# code, it'll start to install necessary tools.
- test code: BubbleSortTest/TestSort.cs
- open xunit code.
- click Yes to message "Required assets to build and debug are missin from ...
- show Inline
- Console Program code: BubbleSorter/Program.cs
- change VSCode dir to the project dir.
- open C# code in the project.
- click Yes to message "Required assets to build and debug are missin from ...
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"
}
]
}- 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",
// set dll path
"program": "${workspaceRoot}/BubbleSorterAPI/bin/Debug/netcoreapp2.0/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.
add settings to launch.json.
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}- launch .NET Core app
cd BubbleSorterAPI
dotnet run- start a debug
- select the process
install vsdbg to remote host (following script install to ~/vsdbg).
curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbgadd 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" : "VSCode(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"
}
}
]
}- 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

