Debugging .NET in VSCode

caraplana1

Cristian Landaez

Posted on March 12, 2022

Debugging .NET in VSCode

Hello and welcome! Today, I'm gonna show you how to use the vscode debugger and how to configure it for all your needs. I hope this article results useful to you and let's go!

Creating the project

First, We need to create a folder called Debugger to save all in there. This is needed to keep the .vscode folder, that holds the debugger files, and have everything in an unique folder.

mkdir Debugger
cd Debugger 
Enter fullscreen mode Exit fullscreen mode

Then, create a net6.0 console project which we will use to see the debugger in action and open vscode.

dotnet new console -n Proj1
code .
Enter fullscreen mode Exit fullscreen mode

Debug your first project.

After opening vscode you should see this in Program.cs file.

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
Enter fullscreen mode Exit fullscreen mode

If not so, the code is ok too. This is just the template for console projects in net6.0, but the debugger works fine with all version of .NET.

Creating debugger files

If you haven't installed the extension for C# yet, you can go to the extension bar, search for C# and install the extension.

If you already have the extension you should see this message after omnisharp finish loading.

message

And if you press Yes the vscode will create the tasks.json and launch.json files to run the debugger.

Or

If you don't see the window you can press Ctrl+shift+p to open the command palette and write .NET: Generate Assests for Build and Debug and execute the command.

Or

Go to the debugger bar and press create a launch.json file
Create launch.json

and select your project target framework. In this case .NET 5+ and .NET Core.
Select TargetFramework

Adding breakpoints

A breakpoint is a flag that you set in a line of code to stop the program before the line is executed.

So, go to Program.cs file and add the following code below the hello world.

for (int i = 1; i < 6; i++)
    Console.WriteLine(i);

Console.WriteLine("Bye, World!");
Enter fullscreen mode Exit fullscreen mode

then go to the line below the for-loop and add a breakpoint by pressing f9.

A red point will show at the left of your editor like this.
Breakpoints

If you press Ctrl+f5 the program will build and execute without the debugger and print the followed in the debug console.

Hello, World!
1
2
3
4
5
Bye, World!
Enter fullscreen mode Exit fullscreen mode

Running the Debugger

If you press f5 the project will be build and after that the debugger will be launch. Then the Debug console will appear. In this console you can also change the variables values. Also will display the debugger bar showing all variables, the call stacks and breakpoints.

Debug bar

And the code will stop at the breakpoint.

Stop code

Playing with the debugger

You will see a highlighted yellow line that show the next line of code to be executed.

You also see this bar
Debugging bar
From left to right, the buttons have this functions:

  1. Continuef5: Continues the execution until the next breakpoint.
  2. Step Overf10: Execute just the next line to be executed.
  3. Step Intof11: jump into the function in the highlighted yellow line.
  4. Step OutShift+f11: jump out of the function to the last stack call.
  5. RestartCtrl+Shift+f5: Restart the the building and re launch the debugger.
  6. StopShift+f5: Stop the program.

The debug console

Also with the debug console you can change the value of the variables.

If you write the following expression in the debug console RELP.

i = -3
Enter fullscreen mode Exit fullscreen mode

You will see in the debugger bar that the variable "i" changes his value from 0 to -3. If you finish executing all the program the final output should be the next.

Hello, World!
-3
-2
-1
0
1
2
3
4
5
Bye, World!
Enter fullscreen mode Exit fullscreen mode

Debugging Multiple Projects

If you ever have using Visual Studio, and had more than one project, you have notice the ease of changing the debugger configuration out of the box. In vscode we need to config the debugger to have something similar.

Setting Up More Projects

Let's create another console project, a solution file and add the projects to the solution with the following lines.

dotnet new console -n Proj2
dotnet new sln
dotnet sln add **/*proj
Enter fullscreen mode Exit fullscreen mode

Tasks.json

Now, we need to modify the tasks.json file, specifically, the build task.
The tasks.json file is used to automate scripts execution and process execution like build, test and publish. We need change the build task so it builds all the solution and not just the Proj1.csproj.

So open the vscode/tasks.json file and you should see this tasks in the file.

{
    "label": "build",
    "command": "dotnet",
    "type": "process",
    "args": [
        "build",
        "${workspaceFolder}/Proj1/Proj1.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
    ],
    "problemMatcher": "$msCompile"
},
Enter fullscreen mode Exit fullscreen mode

And just remove the Proj1.csproj direction from the task, like this.

{
    "label": "build",
    "command": "dotnet",
    "type": "process",
    "args": [
        "build",
        "${workspaceFolder}",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
    ],
    "problemMatcher": "$msCompile"
},
Enter fullscreen mode Exit fullscreen mode

Now vscode builds all the solution and you don't need to build every projects manually.

Add Debug Configuration

Now, you need to add a new configuration to launch.json. Just go to the debug bar and select Add Configuration... like this.
Add Configuration

And will asks you to select what type of configuration you want

Debug config

After you select the configuration change the name of the configuration and the program argument to "${workspaceFolder}//bin/Debug//.dll", like this.

{
    "name": "Proj2(Debug)",
    "type": "coreclr",
    "request": "launch",
    "preLaunchTask": "build",
    "program": "${workspaceFolder}/Proj2/bin/Debug/net6.0/Proj2.dll",
    "args": [],
    "cwd": "${workspaceFolder}",
    "stopAtEntry": false,
    "console": "internalConsole"
},
Enter fullscreen mode Exit fullscreen mode

If you inspected the file for your self you already have notice another arguments like args, to add arguments when the debugger launch; stopAtEntry, if you want to stop at the first line of code of the program and console, to select what console you can use to debug.

And then you can see the configuration added to the debug bar and changed as you wish.
Proj2(Debug)

Conclusions

Well, we learn in this article how to use the debugger and configure by your self for c#. I hope this article helps you with your developer journey.

Thank you for read my article and see you soon!!!

💖 💪 🙅 🚩
caraplana1
Cristian Landaez

Posted on March 12, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Debugging .NET in VSCode
dotnet Debugging .NET in VSCode

March 12, 2022