Skip to content

Commit d2f84d8

Browse files
committed
edits
1 parent 743611b commit d2f84d8

File tree

1 file changed

+121
-118
lines changed

1 file changed

+121
-118
lines changed

articles/cognitive-services/LUIS/luis-tutorial-bot-csharp-appinsights.md

Lines changed: 121 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -42,151 +42,154 @@ In order to capture the LUIS information, the web app bot needs the **[Microsoft
4242
1. Select **Browse** then search for **Microsoft.ApplicationInsights**.
4343
1. Install the package.
4444

45-
## Build and resolve errors
46-
47-
1. Build the solution by selecting the **Build** menu, then select **Rebuild Solution**. Wait for the build to finish.
48-
49-
2. If the build fails with `CS0104` errors, you need to fix them. In the `Controllers` folder, in the `MessagesController.cs file`, fix the ambiguous usage of `Activity` type by prefixing the Activity type with the Connector type. To do this, change the name `Activity` on lines 22 and 36 from `Activity` to `Connector.Activity`. Build the solution again. There should be no more build errors.
50-
51-
The full source of that file is:
52-
53-
[!code-csharp[MessagesController.cs file](~/samples-luis/documentation-samples/tutorial-web-app-bot-application-insights/csharp/MessagesController.cs "MessagesController.cs file")]
54-
55-
## Publish project to Azure
56-
57-
The **Application Insights** package is now in the project and configured correctly for your credentials in the Azure portal. The changes for the project need to be published back to Azure.
58-
59-
1. In the **Solution Explorer**, right-click the project name, then select **Publish**.
60-
61-
![Publish project to portal](./media/luis-tutorial-bot-csharp-appinsights/vs-2017-publish.png)
62-
63-
2. In the **Publish** window, select **Create new profile**.
64-
65-
![As part of publishing, create new profile.](./media/luis-tutorial-bot-csharp-appinsights/vs-2017-publish-1.png)
66-
67-
3. Select **Import profile**, and select **OK**.
68-
69-
![As part of publishing, import profile](./media/luis-tutorial-bot-csharp-appinsights/vs-2017-publish-2.png)
70-
71-
4. In the **Import Publish Settings File** windows, navigate to your project folder, navigate to the `PostDeployScripts` folder, select the file that ends in `.PublishSettings`, and select `Open`. You have now configured publishing for this project.
72-
73-
5. Publish your local source code to Bot Service by selecting the **Publish** button. The **Output** window shows status. The rest of the tutorial is completed in the Azure portal. Close Visual Studio 2017.
74-
75-
## Open three browser tabs
76-
77-
In the Azure portal, find the web app bot and open it. The following steps use three different views of the web app bot. It may be easier to have three separate tabs open in the browser:
78-
79-
> * Test in Web Chat
80-
> * Build/Open online code editor -> App Service Editor
81-
> * App Service Editor/Open Kudu console -> Diagnostic Console
82-
83-
## Modify BasicLuisDialog.cs code
84-
85-
1. In the **App Service Editor** browser tab, open the `BasicLuisDialog.cs` file.
86-
87-
2. Add the following NuGet dependency under the existing `using` lines:
88-
89-
[!code-csharp[Add using statement](~/samples-luis/documentation-samples/tutorial-web-app-bot-application-insights/csharp/BasicLuisDialog.cs?range=11-12 "Add using statement")]
90-
91-
3. Add the `LogToApplicationInsights` function:
92-
93-
[!code-csharp[Add the LogToApplicationInsights function](~/samples-luis/documentation-samples/tutorial-web-app-bot-application-insights/csharp/BasicLuisDialog.cs?range=61-92 "Add the LogToApplicationInsights function")]
94-
95-
The Application Insights instrumentation key is already in the web app bot's application setting named `BotDevInsightsKey`.
96-
97-
The last line of the function adds the data to Application Insights. The trace's name is `LUIS`, a unique name apart from any other telemetry data collected by this web app bot. All the properties are also prefixed with `LUIS_` so you can see what data this tutorial adds compared to information that is given by the web app bot.
98-
99-
4. Call the `LogToApplicationInsights` function at the top of the `ShowLuisResult` function:
100-
101-
[!code-csharp[Use the LogToApplicationInsights function](~/samples-luis/documentation-samples/tutorial-web-app-bot-application-insights/csharp/BasicLuisDialog.cs?range=114-115 "Use the LogToApplicationInsights function")]
102-
103-
## Build web app bot
104-
105-
1. Build the web app bot in one of two ways. The first method is to right-click on `build.cmd` in the **App Service Editor**, then select **Run from Console**. The output of the console displays and completes with `Finished successfully.`
106-
107-
2. If this doesn't complete successfully, you need to open the console, navigate to the script, and run it using the following steps. In the **App Service Editor**, on the top blue bar, select the name of your bot, then select **Open Kudu Console** in the drop-down list.
108-
109-
![Open Kudu Console](./media/luis-tutorial-bot-csharp-appinsights/open-kudu-console.png)
110-
111-
3. In the console window, enter the following command:
112-
113-
```console
114-
cd site\wwwroot && build.cmd
45+
## Capture and send LUIS query results to Application Insights
46+
47+
1. Open the `LuisHelper.cs` file and replace the contents with the following code. The **LogToApplicationInsights** method capture the bot and LUIS data and sends it to Application Insights as a Trace event named `LUIS`.
48+
49+
```csharp
50+
// Copyright (c) Microsoft Corporation. All rights reserved.
51+
// Licensed under the MIT License.
52+
53+
using System;
54+
using System.Linq;
55+
using System.Threading;
56+
using System.Threading.Tasks;
57+
using Microsoft.Bot.Builder;
58+
using Microsoft.Bot.Builder.AI.Luis;
59+
using Microsoft.Extensions.Configuration;
60+
using Microsoft.Extensions.Logging;
61+
using Microsoft.ApplicationInsights;
62+
using System.Collections.Generic;
63+
64+
namespace Microsoft.BotBuilderSamples
65+
{
66+
public static class LuisHelper
67+
{
68+
public static async Task<BookingDetails> ExecuteLuisQuery(IConfiguration configuration, ILogger logger, ITurnContext turnContext, CancellationToken cancellationToken)
69+
{
70+
var bookingDetails = new BookingDetails();
71+
72+
try
73+
{
74+
// Create the LUIS settings from configuration.
75+
var luisApplication = new LuisApplication(
76+
configuration["LuisAppId"],
77+
configuration["LuisAPIKey"],
78+
"https://" + configuration["LuisAPIHostName"]
79+
);
80+
81+
var recognizer = new LuisRecognizer(luisApplication);
82+
83+
// The actual call to LUIS
84+
var recognizerResult = await recognizer.RecognizeAsync(turnContext, cancellationToken);
85+
86+
LuisHelper.LogToApplicationInsights(configuration, turnContext, recognizerResult);
87+
88+
var (intent, score) = recognizerResult.GetTopScoringIntent();
89+
if (intent == "Book_flight")
90+
{
91+
// We need to get the result from the LUIS JSON which at every level returns an array.
92+
bookingDetails.Destination = recognizerResult.Entities["To"]?.FirstOrDefault()?["Airport"]?.FirstOrDefault()?.FirstOrDefault()?.ToString();
93+
bookingDetails.Origin = recognizerResult.Entities["From"]?.FirstOrDefault()?["Airport"]?.FirstOrDefault()?.FirstOrDefault()?.ToString();
94+
95+
// This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part.
96+
// TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year.
97+
bookingDetails.TravelDate = recognizerResult.Entities["datetime"]?.FirstOrDefault()?["timex"]?.FirstOrDefault()?.ToString().Split('T')[0];
98+
}
99+
}
100+
catch (Exception e)
101+
{
102+
logger.LogWarning($"LUIS Exception: {e.Message} Check your LUIS configuration.");
103+
}
104+
105+
return bookingDetails;
106+
}
107+
public static void LogToApplicationInsights(IConfiguration configuration, ITurnContext turnContext, RecognizerResult result)
108+
{
109+
// Create Application Insights object
110+
TelemetryClient telemetry = new TelemetryClient();
111+
112+
// Set Application Insights Instrumentation Key from App Settings
113+
telemetry.InstrumentationKey = configuration["BotDevAppInsightsKey"];
114+
115+
// Collect information to send to Application Insights
116+
Dictionary<string, string> logProperties = new Dictionary<string, string>();
117+
118+
logProperties.Add("BotConversation", turnContext.Activity.Conversation.Name);
119+
logProperties.Add("Bot_userId", turnContext.Activity.Conversation.Id);
120+
121+
logProperties.Add("LUIS_query", result.Text);
122+
logProperties.Add("LUIS_topScoringIntent_Name", result.GetTopScoringIntent().intent);
123+
logProperties.Add("LUIS_topScoringIntentScore", result.GetTopScoringIntent().score.ToString());
124+
125+
126+
// Add entities to collected information
127+
int i = 1;
128+
if (result.Entities.Count > 0)
129+
{
130+
foreach (var item in result.Entities)
131+
{
132+
logProperties.Add("LUIS_entities_" + i++ + "_" + item.Key, item.Value.ToString());
133+
}
134+
}
135+
136+
// Send to Application Insights
137+
telemetry.TrackTrace("LUIS", ApplicationInsights.DataContracts.SeverityLevel.Information, logProperties);
138+
139+
}
140+
}
141+
}
115142
```
116143

117-
Wait for the build to complete with `Finished successfully.`
118-
119-
## Test the web app bot
120-
121-
1. To test your web app bot, open the **Test in Web Chat** feature in the portal.
122-
123-
2. Enter the phrase `Coffee bar on please`.
144+
## Add Application Insights instrumentation key
124145

125-
![Test web app bot in chat](./media/luis-tutorial-bot-csharp-appinsights/test-in-web-chat.png)
146+
In order to add data to application insights, you need the instrumentation key.
126147

127-
3. You should see no difference in the chatbot response. The change is sending data to Application Insights, not in the bot responses. Enter a few more utterances so there is a little more data in Application Insights:
148+
1. In a browser, in the [Azure portal](https://portal.azure.com), find your bot's **Application Insights** resource. Its name will have most of the bot's name, then random characters at the end of the name, such as `luis-nodejs-bot-johnsmithxqowom`.
149+
1. On the Application Insights resource, on the **Overview** page, copy the **Instrumentation Key**.
150+
1. In Visual Studio, open the **appsettings.json** file at the root of the bot project. This file holds all your environment variables.
151+
1. Add a new variable, `BotDevAppInsightsKey` with the value of your instrumentation key. The value in should be in quotes.
128152

129-
|Utterances|
130-
|--|
131-
|Please deliver a pizza|
132-
|Turn off all the lights|
133-
|Turn on the hall light|
153+
## Build and start the bot
134154

155+
1. In Visual Studio, build and run the bot.
156+
1. Start the bot emulator and open the bot. This [step](luis-csharp-tutorial-bf-v4.md#use-the-bot-emulator-to-test-the-bot) is provided in the previous tutorial.
135157

158+
1. Ask the bot a question. This [step](luis-csharp-tutorial-bf-v4.md#ask-bot-a-question-for-the-book-flight-intent) is provided in the previous tutorial.
136159
## View LUIS entries in Application Insights
137160

138-
Open Application Insights to see the LUIS entries.
161+
Open Application Insights to see the LUIS entries. It can take a few minutes for the data to appear in Application Insights.
139162

140-
1. In the portal, select **All resources** then filter by the web app bot name. Click on the resource with the type **Application Insights**. The icon for Application Insights is a light bulb.
163+
1. In the [Azure portal](https://portal.azure.com), open the bot's Application Insights resource.
164+
1. When the resource opens, select **Search** and search for all data in the last **30 minutes** with the event type of **Trace**. Select the trace named **LUIS**.
165+
1. The bot and LUIS information is available under **Custom Properties**.
141166

142-
![Search for app insights in the Azure portal](./media/luis-tutorial-bot-csharp-appinsights/portal-service-list-app-insights.png)
143-
144-
2. When the resource opens, click on the **Search** icon of the magnifying glass in the far right panel. A new panel to the right displays. Depending on how much telemetry data is found, the panel may take a second to display. Search for `LUIS`. The list is narrowed to just LUIS query results added with this tutorial.
145-
146-
![Search for traces](./media/luis-tutorial-bot-csharp-appinsights/portal-service-list-app-insights-search-luis-trace.png)
147-
148-
3. Select the top entry. A new window displays more detailed data including the custom data for the LUIS query at the far-right. The data includes the top intent, and its score.
149-
150-
![Review trace item](./media/luis-tutorial-bot-csharp-appinsights/portal-service-list-app-insights-search-luis-trace-item.png)
151-
152-
When you are done, select the far-right top **X** to return to the list of dependency items.
153-
154-
> [!Tip]
155-
> If you want to save the dependency list and return to it later, click on **...More** and click **Save favorite**.
167+
![Review LUIS custom properties stored in Application Insights](./media/luis-tutorial-appinsights/application-insights-luis-trace-custom-properties.png)
156168

157169
## Query Application Insights for intent, score, and utterance
170+
Application Insights gives you the power to query the data with the [Kusto](https://docs.microsoft.com/azure/application-insights/app-insights-analytics#query-data-in-analytics) language, as well as export it to [Power BI](https://powerbi.microsoft.com).
158171
159-
Application Insights gives you the power to query the data with the [Kusto](https://docs.microsoft.com/azure/application-insights/app-insights-analytics#query-data-in-analytics) language, as well as export it to [Power BI](https://powerbi.microsoft.com).
160-
161-
1. Click on **Analytics** at the top of the dependency listing, above the filter box.
162-
163-
![Analytics button](./media/luis-tutorial-bot-csharp-appinsights/portal-service-list-app-insights-search-luis-analytics-button.png)
164-
165-
2. A new window opens with a query window at the top and a data table window below that. If you have used databases before, this arrangement is familiar. The query includes all items from the last 24 hours beginning with the name `LUIS`. The **CustomDimensions** column has the LUIS query results as name/value pairs.
166-
167-
![Default analytics report](./media/luis-tutorial-bot-csharp-appinsights/analytics-query-1.png)
168-
169-
3. To pull out the top intent, score, and utterance, add the following just above the last line in the query window:
172+
1. Select **Log Analytics**. A new window opens with a query window at the top and a data table window below that. If you have used databases before, this arrangement is familiar. The query represents your previous filtered data. The **CustomDimensions** column has the bot and LUIS information.
173+
1. To pull out the top intent, score, and utterance, add the following just above the last line (the `|top...` line) in the query window:
170174

171175
```kusto
172-
| extend topIntent = tostring(customDimensions.LUIS_topScoringIntent)
173-
| extend score = todouble(customDimensions.LUIS_topScoringIntentScore)
174-
| extend utterance = tostring(customDimensions.LUIS_query)
176+
| extend topIntent = tostring(customDimensions.LUIS_luisResponse_luisResult_topScoringIntent_intent)
177+
| extend score = todouble(customDimensions.LUIS_luisResponse_luisResult_topScoringIntent_score)
178+
| extend utterance = tostring(customDimensions.LUIS_luisResponse_text)
175179
```
176180

177-
4. Run the query. Scroll to the far right in the data table. The new columns of topIntent, score, and utterance are available. Click on the topIntent column to sort.
181+
1. Run the query. The new columns of topIntent, score, and utterance are available. Select topIntent column to sort.
178182

179-
![Custom analytics report](./media/luis-tutorial-bot-csharp-appinsights/analytics-query-2.png)
183+
Learn more about the [Kusto query language](https://docs.microsoft.com/azure/log-analytics/query-language/get-started-queries) or [export the data to Power BI](https://docs.microsoft.com/azure/application-insights/app-insights-export-power-bi).
180184
181-
Learn more about the [Kusto query language](https://docs.microsoft.com/azure/log-analytics/query-language/get-started-queries) or [export the data to Power BI](https://docs.microsoft.com/azure/application-insights/app-insights-export-power-bi).
182185

183186
## Learn more about Bot Framework
184187

185188
Learn more about [Bot Framework](https://dev.botframework.com/).
186189
187190
## Next steps
188191

189-
Other information you may want to add to the application insights data includes app ID, version ID, last model change date, last train date, last publish date. These values can either be retrieved from the endpoint URL (app ID and version ID), or from an [authoring API](https://westus.dev.cognitive.microsoft.com/docs/services/5890b47c39e2bb17b84a55ff/operations/5890b47c39e2bb052c5b9c3d) call then set in the web app bot settings and pulled from there.
192+
Other information you may want to add to the application insights data includes app ID, version ID, last model change date, last train date, last publish date. These values can either be retrieved from the endpoint URL (app ID and version ID), or from an authoring API call then set in the web app bot settings and pulled from there.
190193

191194
If you are using the same endpoint subscription for more than one LUIS app, you should also include the subscription ID and a property stating that it is a shared key.
192195

0 commit comments

Comments
 (0)