Anil Nivargi
Anil Nivargi

Reputation: 1717

Azure application insights showing duplicate log entry for Node js application

We have a node js application and configured application insight using application insight key in launch.json, but in azure application insight showing duplicate log entry, I tried in google and also stackoverflow but unable to solve the issue.

My appInsights.js code is as follows,

const appInsightsObj = require("applicationinsights");

module.exports.initAppInsights = (appInsightKey, appName) => {
appInsightsObj.setup(appInsightKey)
    .setAutoDependencyCorrelation(true)
    .setAutoCollectRequests(true)
    .setAutoCollectPerformance(true, true)
    .setAutoCollectExceptions(true)
    .setAutoCollectDependencies(true)
    .setAutoCollectConsole(true, true)
    .setUseDiskRetryCaching(true)
    .setSendLiveMetrics(true)
    .setDistributedTracingMode(appInsightsObj.DistributedTracingModes.AI_AND_W3C);
appInsightsObj.defaultClient.context.tags[appInsightsObj.defaultClient.context.keys.cloudRole] = appName;
appInsightsObj.start();
}

module.exports.trackEvent = (name, properties) => {
   const telemetry = appInsightsObj.defaultClient
   telemetry.trackEvent({
      "name": name,
       "properties": properties
 })
}
module.exports.insightObj = appInsightsObj;

and log.js file is as follows,

 const { appConfig } = require('../../config');
 const appInsights = require('./appinsights')
 const {AzureApplicationInsightsLogger}  = require('winston-azure-application-insights');
 const log = require('winston');
 require('winston-daily-rotate-file');
 const path = require('path')
 const _ = require('lodash');
 appInsights.initAppInsights(appConfig.get("appinsights_key"), appConfig.get("app_name"));

 var transportRotatingFile = new log.transports.DailyRotateFile({
    filename: 'loyalty_service_%DATE%.log',
    dirname: appConfig.get('logfile_location'),
    datePattern: 'YYYY-MM-DD',
    zippedArchive: true,
    maxSize: '20m',
    maxFiles: '14d'
 }); 
 transportRotatingFile.on('rotate', function(oldFilename, newFilename) {
 // any code while rotating log files.
});

 log.add (new AzureApplicationInsightsLogger({client: appInsights.insightObj.defaultClient}))
 log.add(new (log.transports.Console)({level: 'info', 'timestamp':true, 'json': false}))
 log.add(transportRotatingFile)
 module.exports = log;

In launch.json defined the appInsightKey and value and based on that it should read and connect to the application insights.

Azure application insights log entry, enter image description here

Can anyone suggest me what should be the cause ? and how to resolve the issue.

Upvotes: 1

Views: 1774

Answers (1)

Anil Nivargi
Anil Nivargi

Reputation: 1717

Finally I found the cause and solution.

Cause - We are using both winston-azure-application-insights and applicationinsights. Both serves the same functionality, so in azure application can see duplicate entries.

Solution:- when I comment winston-azure-application-insights code changes it worked fine, can not see duplicate entries in the application insights.

in log.js, commented below line,

//log.add (new AzureApplicationInsightsLogger({client: appInsights.insightObj.defaultClient}))
log.add(new (log.transports.Console)({level: 'info', 'timestamp':true, 'json': false}))
log.add(transportRotatingFile)
module.exports = log;

Upvotes: 1

Related Questions