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”:目标进程将在自己的外部终端内运行。