How do I configure Azure AppInsights with all routes to log exceptions and console statements?
This is appInsights.js:
const appInsights = require('applicationinsights');
appInsights.setup(process.env.APPLICATIONINSIGHTS_CONNECTION_STRING)
.setAutoCollectConsole(true, true)
.setAutoCollectExceptions(true)
.start();
module.exports = appInsights
routes/index.js:
var express = require('express');
var router = express.Router();
const { appInsights } = require('../services/appInsights');
const { botService } = require('../services/bot');
router.get('/', function (req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/api/messages', async function (req, res, next) {
console.log("--inside /messages--")
try {
//call a method from bot.js
});
}
catch (error) {
console.error('Error in /messages:', error);
appInsightsClient.trackException({ exception: err });
res.status(500).send('Internal Server Error');
}
Currently, everything is being logged as “trace” in App Insights and the exceptions are not shown separately even though I’ve used
appInsightsClient.trackException({ exception: err });
in index.js and I’m also unsure how to use this in bot.js. I’ve tried importing appInsights again in Bot.js, but that results in duplication of logs. I’m familiar with AWS where I just create a lambda and it has Cloudwatch logs by default and this is my first time working on Azure.
Matthew Holliday is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.