Adding Logging to an Existing bot
Follow these steps:
Connect to Application Insights
- Create an Application Insights service under your subscription.
- Use the
Instrumentation Key
inside your bot registration page under Instrumentation key. - Under the App Insights serivce, go to API Access and copy Application ID
- Under the App Insights serivce, go to API Access >> New Key with Read permissions and copy Api Key.
Connect to Cognitive Services
Create a new Sentiment Analisys Service under Cognitive Services.
When creating the service, make sure to mark Text Analytics - Preview.
Setting Environment Variables
APPINSIGHTS_INSTRUMENTATIONKEY={App Insights Instrumentation Key}
CG_SENTIMENT_KEY={Cognitive Services Text Analytics Key}
Connecting to Code
var instrumentation = require('botbuilder-instrumentation');
let logging = new instrumentation.BotFrameworkInstrumentation({
instrumentationKey: process.env.APPINSIGHTS_INSTRUMENTATIONKEY,
sentimentKey: process.env.CG_SENTIMENT_KEY,
});
logging.monitor(bot);
Using mutiple instrumnetation keys
var instrumentation = require('botbuilder-instrumentation');
let logging = new instrumentation.BotFrameworkInstrumentation({
instrumentationKey: ["main insturmnation key","secondary instumentation key"],
sentimentKey: process.env.CG_SENTIMENT_KEY,
});
logging.monitor(bot);
Sending logs for QnA maker service
var instrumentation = require('botbuilder-instrumentation');
let logging = new instrumentation.BotFrameworkInstrumentation({
instrumentationKey: ["main insturmnation key","secondary instumentation key"],
sentimentKey: process.env.CG_SENTIMENT_KEY,
});
logging.monitor(bot);
loggins.trackQNAEvent(context, userQuery, kbQuestion, kbAnswer, score);
Sending custom event data
var instrumentation = require('botbuilder-instrumentation');
let logging = new instrumentation.BotFrameworkInstrumentation({
instrumentationKey: ["main insturmnation key","secondary instumentation key"],
sentimentKey: process.env.CG_SENTIMENT_KEY,
});
logging.monitor(bot);
let customEventName = 'myCustomEventName';
let customEventData = { customeDataA: 'customValueA', customDataB: 3 };
logging.trackCustomEvent(context, customEventName, customEventData);
You can see a working sample in https://github.com/morsh/bot-with-instrumentation