Visual Studio Code on its own is just a lightweight code editor however Omnisharp's C# extension includes debugging capabilities that makes VsCode a productive C# development environment.

Prerequisites

To write and debug C# applications on Linux the following prerequisites must be installed:

Enabling Debugging

When opening a new C# project for the first time in VsCode a popup window should display asking if you would like to add debug assets to your project - Select Yes when you see this message.

You should be able to see a new .vscode folder in your project which contains 2 files - tasks.json and launch.json.
The Tasks.json file should be populated with configuration data for the dotnet build task so that VsCode can build out project when we click the launch button.

{
    "version": "0.1.0",
    "command": "dotnet",
    "isShellCommand": true,
    "args": [],
    "tasks": [
        {
            "taskName": "build",
            "args": [
                "${workspaceRoot}/newconsole.csproj"
            ],
            "isBuildCommand": true,
            "problemMatcher": "$msCompile"
        }
    ]
}

The Launch.json file contains configuration data for the available debugging modes which by default are launch (from VsCode) and attach (to a running process).

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceRoot}/bin/Debug/netcoreapp1.1/newconsole.dll",
            "args": [],
            "cwd": "${workspaceRoot}",
            "console": "internalConsole",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

Launching the Debugger from VsCode

Launching the debugger from VsCode is a very simple process for Console and Web projects, simply set a breakpoint in your code, navigate to the debug window (ctrl+shift+d) and hit the play button - the ".Net Core Launch" option should be selected by default. Your application should now stop at your breakpoints.

Attach to a process/website

Attaching the debugger to a running process is also very easy with VsCode, set your breakpoints, select the ".Net Core Attach" option from the debug menu and hit play. The task bar should show a list of running processes you can choose to attach your debugger to - in this example we shall attach to a running dotnet website process.

Exploring the Debug panes

The debug window in VsCode may seem heavily stripped down compared to the fully fledged version of Viual Studio however it provides enough functionality to cover the majority of use cases with the following panes:

  • Local Variables
  • Watch Window
  • Call Stack
  • Breakpoints

The local variable and watch windows provide essential variable tracking and modification functions while the watch window also enables the execution of methods on the fly including LINQ extension methods which is extremely useful.

The call stack window functions the same as its full fledged Visual Studio counterpart enabling crawling back up through the code execution heirarchy with full variable tracking.

Debuging on non-standard linux distros

If you've been following the older posts on this blog you will have noticed that I'm using VsCode on my Chromebook which is running a non-standard Liux Distribution based on Ubuntu 16.04 - GalliumOS. As of writing this article, Omnisharp executes its debugger assembly based on the identifier of the operating system running VsCode (in my case galliumos-2.1: an unrecognized operating system).

Luckily for us VsCode and all its extensions are written in Javascript meaning that we can get around this by editing a *.js file.

Find the location of your omnisharp extension on your machine and open the platform.js file. On my machine the path is:

/home/myusername/.vscode/extensions/ms-vscode.csharp-1.8.1/out/src/platform.js

In here there is a function called getRuntimeIdHelper which dictates what distribution to use. To get it to work with GalliumOS v2.1 I just had to add a new case block to the switch statement to handle GalliumOS and now C# debugging works with GalliumOS.

case 'galliumos':  
    return ubuntu_16_04;
    break;

I hope this method works for others building .net core apps using other non-standard Lunix distributions.