Skip to content

.NET watch with debugger attaching for Visual Studio Code

It’s not that simple to automatically attach a debugger to a process. The watch task never ends, so we need to signal the VSC when the program is initialized and the debugger can be attached. The simplest way to achieve this is to write a message to the console and listen for it in the launch.json configuration. The following example shows how to do this.

tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/Path/To/.cproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": {
"pattern": "$msCompile",
"background": {
"activeOnStart": true,
"beginsPattern": "^.*",
"endsPattern": ""
}
},
"isBackground": true
}
]
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach",
"type": "coreclr",
"request": "attach",
"processName": "NameOfTheProcess.exe",
"preLaunchTask": "watch"
}
]
}