title | description | author | ms.author | keywords | manager | ms.topic | ms.service | ms.subservice | ms.date |
---|---|---|---|---|---|---|---|---|---|
Debug a bot | Microsoft Docs |
Learn how to debug a bot built using Bot Service. |
v-ducvo |
v-ducvo |
Bot Framework SDK, debug bot, test bot, bot emulator, emulator |
kamrani |
article |
bot-service |
sdk |
11/13/2018 |
This article describes how to debug your bot using an integrated development environment (IDE) such as Visual Studio or Visual Studio Code and the Bot Framework Emulator. While you can use these methods to debug any bot locally, this article uses a C# and JS bot created in the quickstart.
- Download and install the Bot Framework Emulator.
- Download and install Visual Studio Code or Visual Studio (Community Edition or above).
To run a JavaScript bot using the command line and testing the bot with the emulator, do the following:
-
From the command line, change directory to your bot project directory.
-
Start the bot by running the command node app.js.
-
Start the emulator and connect to the bot's endpoint (e.g.: http://localhost:3978/api/messages). If this is the first time you are running the bot then click File > New Bot and follow the instructions on screen. Otherwise, click File > Open Bot to open an existing bot. Since this bot is running locally on your computer, you can leave the MSA app ID and MSA app password fileds blank. For more information, see Debug with the Emulator.
-
From the emulator, send your bot a message (e.g.: send the message "Hi").
-
Use the Inspector and Log panels on the right side of the emulator window to debug your bot. For example, clicking on any of the messages bubble (e.g.: the "Hi" message bubble in the screenshot below) will show you the detail of that message in the Inspector panel. You can use it to view requests and responses as messages are exchanged between the emulator and the bot. Alternatively, you can click on any of the linked text in the Log panel to view the details in the Inspector panel.
In Visual Studio Code, you can set breakpoints and run the bot in debug mode to step through your code. To set breakpoints in VS Code, do the following:
- Launch VS Code and open your bot project folder.
- From the menu bar, click Debug and click Start Debugging. If you are prompted to select a runtime engine to run your code, select Node.js. At this point, the bot is running locally.
-
Set breakpoint as necesary. In VS Code, you can set breakpoints by hovering your mouse over the column to the left of the line numbers. A small red dot will appear. If you click on the dot, the breakpoint is set. If you click the dot again, the breakpoint is removed.
-
Start the Bot Framework Emulator and connect to your bot as described in the section above.
-
From the emulator, send your bot a message (e.g.: send the message "Hi"). Execution will stop at the line where you place the breakpoint.
In Visual Studio (VS), you can set breakpoints and run the bot in debug mode to step through your code. To set breakpoints in VS, do the following:
- Navigate to your bot folder and open the .sln file. This will open the solution in VS.
- From the menu bar, click Build and click Build Solution.
- In the Solution Explorer, click EchoWithCounterBot.cs. This file defines your main bot logic.Set breakpoint as necesary. In VS, you can set breakpoints by hovering your mouse over the column to the left of the line numbers. A small red dot will appear. If you click on the dot, the breakpoint is set. If you click the dot again, the breakpoint is removed.
- From the menu bar, click Debug and click Start Debugging. At this point, the bot is running locally.
-
Start the Bot Framework Emulator and connect to your bot as described in the section above.
-
From the emulator, send your bot a message (e.g.: send the message "Hi"). Execution will stop at the line where you place the breakpoint.
::: moniker range="azure-bot-service-3.0"
The Consumption plan serverless C# environment in Bot Service has more in common with Node.js than a typical C# application because it requires a runtime host, much like the Node engine. In Azure, the runtime is part of the hosting environment in the cloud, but you must replicate that environment locally on your desktop.
Before you can debug your Consumption plan C# bot, you must complete these tasks.
- Download the source code for your bot (from Azure), as described in Set up continuous deployment.
- Download and install the Bot Framework Emulator.
- Install the Azure Functions CLI.
- Install the DotNet CLI.
If you want to be able to debug your code by using breakpoints in Visual Studio 2017, you must also complete these tasks.
- Download and install Visual Studio 2017 (Community Edition or above).
- Download and install the Command Task Runner Visual Studio Extension.
Note
Visual Studio Code is not currently supported.
The simplest way to debug your bot locally is to start the bot and then connect to it from Bot Framework Emulator.
First, open a command prompt and navigate to the folder where the project.json file is located in your repository. Then, run the command dotnet restore
to restore the various packages that are referenced in your bot.
Note
Visual Studio 2017 changes how Visual Studio handles dependencies.
While Visual Studio 2015 uses project.json to handle dependencies,
Visual Studio 2017 uses a .csproj model when loading in Visual Studio.
If you are using Visual Studio 2017, download this .csproj file
to the /messages folder in your repository before you run the dotnet restore
command.
Next, run debughost.cmd
to load and start your bot.
At this point, the bot is running locally. From the console window, copy the endpoint that debughost is listening on (in this example, http://localhost:3978
). Then, start the Bot Framework Emulator and paste the endpoint into the address bar of the emulator. For this example, you must also append /api/messages
to the endpoint. Since you do not need security for local debugging, you can leave the Microsoft App ID and Microsoft App Password fields blank. Click Connect to establish a connection to your bot using the specified endpoint.
After you have connected the emulator to your bot, send a message to your bot by typing some text into the textbox that is located at the bottom of the emulator window (i.e., where Type your message... appears in the lower-left corner). By using the Log and Inspector panels on the right side of the emulator window, you can view the requests and responses as messages are exchanged between the emulator and the bot.
Additionally, you can view log details in the console window.
::: moniker-end
[!div class="nextstepaction"] Debug your bot using transcript files.