Debug a bot
In this article, we use the Bot Framework Emulator to send and receive messages from the bot during debugging. If you are looking for other ways to debug your bot using the Bot Framework Emulator, please read the Debug with the Bot Framework Emulator article.
- Download and install the Bot Framework Emulator.
- Download and install Visual Studio Code or Visual Studio (Community Edition or above).
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.
- Click the .js file and set breakpoints as necessary. 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 Debug with the Bot Framework Emulator article.
- From the emulator, send your bot a message (for example, send the message "Hi"). Execution will stop at the line where you place the breakpoint.
Debug a C# bot using breakpoints in Visual Studio
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 the .cs file and set breakpoints as necessary. This file defines your main bot logic. 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, 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.
Debug a Consumption plan C# Functions bot
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.
Visual Studio Code is not currently supported.
Debug a Consumption plan C# Functions bot using the emulator
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.
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.
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.
- See troubleshoot general problems and the other troubleshooting articles in that section.
- See the how to Debug with the Emulator.