Dotnet watch with debug Visual Studio Code

Dotnet watch with debug Visual Studio Code

To use dotnet watch with debugging in Visual Studio Code, follow these steps:

  1. Open your project in Visual Studio Code.
  2. Open the Debug view by clicking on the Debug icon in the Activity Bar on the side of the screen.
  3. Click on the gear icon to create a new launch configuration and select ".NET Core Launch (console)".
  4. In the launch.json file that opens, replace the contents with the following:
{
    "name": ".NET Core Watch",
    "type": "coreclr",
    "request": "launch",
    "preLaunchTask": "build",
    "program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/<project-name>.dll",
    "args": [],
    "cwd": "${workspaceFolder}",
    "stopAtEntry": false,
    "console": "internalConsole",
    "env": {
        "ASPNETCORE_ENVIRONMENT": "Development"
    },
    "sourceFileMap": {
        "/Views": "${workspaceFolder}/Views"
    },
    "pipeTransport": {
        "pipeCwd": "${workspaceFolder}",
        "pipeProgram": "dotnet",
        "pipeArgs": [
            "watch",
            "run"
        ],
        "debuggerPath": "/usr/share/dotnet/shared/Microsoft.NETCore.App/3.1.7/libdbgshim.so"
    }
}
  1. Replace <project-name> with the name of your project.
  2. Save the launch.json file.
  3. In the terminal, navigate to the root of your project and run dotnet watch run.
  4. In the Debug view, select ".NET Core Watch" from the drop-down list of launch configurations.
  5. Click the green play button to start debugging.

This configuration will launch your application with dotnet watch and attach the debugger to it. You can set breakpoints and step through your code as usual.

Examples

  1. "dotnet watch debug Visual Studio Code"

    • Code:
      "configurations": [
        {
          "name": ".NET Core Launch (web)",
          "type": "coreclr",
          "request": "launch",
          "preLaunchTask": "build",
          "program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/YourApp.dll",
          "args": [],
          "cwd": "${workspaceFolder}",
          "stopAtEntry": false,
          "serverReadyAction": {
            "action": "openExternally",
            "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
          },
          "internalConsoleOptions": "openOnSessionStart",
          "launchBrowser": {
            "enabled": true,
            "args": "${auto-detect-url}",
            "windows": {
              "command": "cmd.exe",
              "args": "/C start ${auto-detect-url}"
            },
            "osx": {
              "command": "open"
            },
            "linux": {
              "command": "xdg-open"
            }
          },
          "sourceFileMap": {
            "/Views": "${workspaceFolder}/Views"
          }
        }
      ]
      
    • Description: Configure the launch.json file in Visual Studio Code to enable debugging with dotnet watch. Adjust the paths and settings based on your project structure.
  2. "dotnet watch Visual Studio Code launch.json tasks"

    • Code:
      "tasks": [
        {
          "label": "build",
          "command": "dotnet",
          "type": "process",
          "args": [
            "build",
            "${workspaceFolder}/YourApp.csproj"
          ],
          "problemMatcher": "$msCompile"
        }
      ]
      
    • Description: Define a build task in the tasks.json file to build your .NET Core application before starting debugging with dotnet watch.
  3. "dotnet watch debug attach to process Visual Studio Code"

    • Code:
      "configurations": [
        {
          "name": ".NET Core Attach",
          "type": "coreclr",
          "request": "attach",
          "processId": "${command:pickProcess}"
        }
      ]
      
    • Description: Configure an "Attach" configuration in launch.json to attach the Visual Studio Code debugger to a running dotnet watch process.
  4. "dotnet watch debug external console Visual Studio Code"

    • Code:
      "internalConsoleOptions": "neverOpen"
      
    • Description: Set "internalConsoleOptions" to "neverOpen" in your launch.json file to prevent the internal console from opening when debugging with dotnet watch.

More Tags

jq sqldatatypes firebaseui interruption timeout android-drawable .htaccess inotifypropertychanged angular2-pipe semantic-ui

More C# Questions

More Everyday Utility Calculators

More Housing Building Calculators

More Tax and Salary Calculators

More General chemistry Calculators