C# for Visual Studio Code

repository·main·Indexed 25 days ago

https://github.com/dotnet/vscode-csharp

Extension providing language support for C# using the Language Server Protocol (LSP), integrating with Roslyn and Razor for IntelliSense, refactoring, and navigation. It includes guides for .NET debugging, remote debugging via SSH or Docker, and configuration for legacy .NET Framework projects using OmniSharp.

Tokens
19.2K
Snippets
48
Records
115
Agent score
85%

What's inside C# for Visual Studio Code

  1. C# Extension Features Overview

    main

    The C# extension provides several language support features powered by Roslyn and Razor:

    • Refactoring: Code fixes and refactorings.
    • Navigation: Go To Definition and Find All References.
    • IntelliSense: Auto-completion while writing code.
    • Formatting and Linting: Code formatting and linting support.
  2. Retrieve OmniSharp or C# logs for troubleshooting

    main

    When reporting an issue, you should include the OmniSharp or C# logs from the VS Code Output pane.

    1. Open the Output pane in VS Code (press Ctrl+Shift+U or navigate to View -> Output).
    2. In the output pane's dropdown menu on the right, select OmniSharp log or C#.
    3. Copy the log content and paste it into the corresponding section of your GitHub issue.
  3. Capture diagnostic logs and traces

    main

    The C# extension provides a command to collect logs, activity logs, traces, and dumps into a single .zip archive. This is the recommended way to provide diagnostic information when reporting issues.

    1. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
    2. Select CSharp: Collect C# Logs (csharp.collectLogs).
    3. Use the multi-select picker to choose additional data:
      • Record Activity: Captures live C# (including Razor) and LSP trace output.
      • Performance Trace: Records a dotnet-trace of the language server.
      • Memory Dump: Processes a memory dump using dotnet-dump.
      • GC Dump: Processes a garbage collector heap dump using dotnet-gcdump.
    4. Customize arguments if prompted, choose a save location, and then reproduce the issue while the recording is active.
    5. Click Cancel on the notification to stop recording.

    Warning: Logs may contain file paths, project names, and workspace information. Review them before sharing publicly.

    csharp.collectLogs
  4. Enable C# debugger logging via VS Code Settings

    main

    To troubleshoot debugger issues, you can enable logging through the VS Code settings editor. The most useful setting is Protocol Messages. Once enabled, logs will be sent to the VS Code Debug Console.

    1. Open the VS Code settings editor (File -> Preferences -> Settings).
    2. Search for diagnosticsLog in the search bar.
    3. Navigate to Extensions -> C# -> Debugger in the settings tree.
    4. Enable the required logging setting (e.g., Protocol Messages).
  5. Revert to OmniSharp

    main

    If you prefer to use OmniSharp instead of the modern Language Server features, perform the following actions:

    1. Go to the Extension settings and set dotnet.server.useOmnisharp to true.
    2. Uninstall or disable C# Dev Kit.
    3. Restart VS Code.
  6. Install VSDBG on a remote server

    main

    VSDBG (the .NET Core command line debugger) must be installed on the target machine.

    Using curl:

    curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l ~/vsdbg

    Using wget:

    wget https://aka.ms/getvsdbgsh -O - 2>/dev/null | /bin/sh /dev/stdin -v latest -l ~/vsdbg

    Using PowerShell (to download on Windows and move to Linux/Mac/Container): Supported RuntimeID values include linux-x64, linux-musl-x64, linux-arm, and osx.

  7. Enable debugging into the .NET Runtime

    main

    To debug into the .NET Runtime or Microsoft-provided open-source libraries, you must configure your launch.json file with specific debugger settings. This allows the debugger to download symbols from the Microsoft Symbol Server and prevents the runtime from using optimized code that would make stepping through source code difficult.

    If you do not have a launch.json file, generate one by running .NET: Generate Assets for build and debug from the VS Code command palette.

    Note: If your launch.json already contains an env property, add the COMPlus_ReadyToRun variable to the existing block instead of creating a duplicate env property.

    "justMyCode": false,
    "symbolOptions": {
        "searchMicrosoftSymbolServer": true
    },
    "suppressJITOptimizations": true,
    "env": {
        "COMPlus_ReadyToRun": "0"
    }
  8. Debug into open-source NuGet packages

    main

    To debug into external open-source libraries hosted on NuGet.org (e.g., Newtonsoft.Json), enable the searchNuGetOrgSymbolServer option in your launch.json.

    Usage Notes:

    • Optimizations: Most NuGet libraries are not ahead-of-time (AOT) compiled. If you are only debugging a NuGet library and not the .NET Runtime itself, you can omit the env section (COMPlus_ReadyToRun: "0") to improve performance.
    • Symbol Availability: Not all libraries on NuGet.org have indexed PDB files. If symbols cannot be found, the library author may need to upload symbol packages (.snupkg).
    • Microsoft Server: If you are only interested in OSS libraries and not Microsoft-specific libraries, you can omit searchMicrosoftSymbolServer.
    "justMyCode": false,
    "symbolOptions": {
        "searchNuGetOrgSymbolServer": true,
        "searchMicrosoftSymbolServer": true
    },
    "suppressJITOptimizations": true,
    // NOTE: Remove unless debugging into the .NET Runtime
    "env": {
        "COMPlus_ReadyToRun": "0"
    }
  9. Set up SSH for remote debugging

    main

    To attach to a remote process via SSH, you must configure passwordless authentication using SSH keys.

    1. Client Machine Setup: Generate an RSA key pair in your local ~/.ssh directory.

    2. Target Machine Setup:

    • Install an SSH server (e.g., sudo apt-get install openssh-server on Ubuntu).
    • Add your client's public key (id_rsa.pub) to the ~/.ssh/authorized_keys file on the server.

    3. Verify Connection: Test the connection from your client terminal:

    ssh ExampleAccount@ExampleTargetComputer echo "Hello World"
    # Client setup
    mkdir ~/.ssh
    chmod 700 ~/.ssh
    ssh-keygen -t rsa
    
    # Server setup (Ubuntu example)
    sudo apt-get install openssh-server
    
    # Test connection
    ssh ExampleAccount@ExampleTargetComputer echo "Hello World"
  10. Troubleshoot colorization and syntax highlighting

    main

    If you encounter incorrect syntax highlighting, provide the following to help diagnose:

    1. The name of the VS Code theme you are using.
    2. Token and Scope information:
      • Open the Command Palette and run Developer: Inspect Editor Tokens and Scopes (editor.action.inspectTMScopes).
      • Click the problematic text to see its token/scope details.
      • Provide a screenshot of this information in your report.
    editor.action.inspectTMScopes
  11. Prepare application and PDBs for remote debugging

    main

    To ensure successful remote debugging, follow these requirements:

    1. PDB Files: VSDBG requires PDB files to map executable code to source code.
      • If building on the target server, PDBs are typically handled automatically.
      • If building elsewhere, copy PDB files next to their associated .dll files, or set DebugType to embedded in your project to keep PDB data inside the .dll.
    2. Configuration: For the best experience, run the Debug configuration of your application on the remote target.
    3. Release Mode: If you must debug a Release configuration, you must disable justMyCode in your launch.json.