launch.json
這個launch.json啓動文件用於在Visual Studio Code中配置調試器。
Visual Studio Code生成launch.json啓動幾乎所有需要的信息。如果您的工作區只有一個可啓動的項目,則 C# extension將提供自動生成此文件的功能。如果錯過此提示,可以通過從VS Code命令選項板執行命令.NET:Generate Assets for Build and Debug來強制生成。
生成的launch.json文件包含兩個部分,一個用於配置啓動launch調試,另一個用於配置附加attatch調試。
下面這個例子是一個僅一個C#工程項目的自動生成的launch.json文件:
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/vscode_test6.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
如果有多個可啓動的項目,則需要手動修改launch.json啓動文件。Visual Studio Code仍然會生成一個基本模板,但您需要填寫“program”字段以指向要調試的可執行dll。
C#工程各配置參數
PreLaunchTask
preLaunchTask字段設置一個taskName,這個taskName是在tasks.json配置文件中配置,它在調試任務之前運行。我們可以通過執行命令Tasks:Configure Tasks Runner從VS Code命令面板獲得默認的build prelaunch任務。
這將創建運行dotnet build的任務。有關任務的詳細信息,請訪問https://code.visualstudio.com/docs/editor/tasks。
Program
Program字段設置爲要啓動的應用程序dll或.NET核心主機可執行文件的路徑。此屬性的格式通常爲:
“${workspaceFolder}/bin/Debug//<project-name.dll>".
示例:
“${workspaceFolder}/bin/Debug/netcoreapp1.1/MyProject.dll"
其中:
<target framework>是正在爲其生成調試項目的框架。這通常在項目.csproj文件中作爲“TargetFramework”屬性找到。
<project-name.dll>調試項目的生成輸出dll的名稱。這通常與項目文件名相同,多一個.dll的文件擴展名爲。
Cwd
目標進程的工作目錄。
Args
這些是將傳遞給程序的參數。
stopAtEntry
如果需要在目標的入口點停止,可以選擇將stopAtEntry設置爲“true”。
Environment variables
可以使用此架構將環境變量傳遞給程序。
"env": {
"myVariableName":"theValueGoesHere"
}
Starting a Web Browser
默認的ASP.NET項目的launch.json啓動模板(從C# extension1.20.0起)使用下面的配置啓動瀏覽器:
"serverReadyAction": {
"action": "openExternally",
"pattern": "^\\s*Now listening on:\\s+(https?://\\S+)"
}
Console (terminal) window
“控制檯”設置控制目標應用程序啓動到哪個控制檯(終端)窗口。它可以設置爲這些值中的任何一個:
“internalConsole”(默認):目標進程的控制檯輸出(stdout/stderr)轉到VS Code調試控制檯。這對於從網絡、文件等獲取輸入的可執行文件非常有用,但對於希望從控制檯讀取的應用程序(例如:Console.ReadLine)則無法工作。
“integratedTerminal”:目標進程將在VS代碼的集成終端內運行。單擊編輯器下方選項卡組中的“Terminal”選項卡以與應用程序交互。或者添加“internalConsoleOptions”:“neverOpen”使其成爲默認的前臺選項卡是終端選項卡。
“externalTerminal”:目標進程將在自己的外部終端內運行。