Azure application insights showing duplicate log entry for Node js application

Anil Nivargi 26 Reputation points
2022-07-11T05:14:40.443+00:00

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,

219393-appinsightslogs.jpg

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

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,247 questions
Azure App Configuration
Azure App Configuration
An Azure service that provides hosted, universal storage for Azure app configurations.
229 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,751 questions
{count} vote

Accepted answer
  1. Monalla-MSFT 12,946 Reputation points
    2022-07-13T13:38:36.367+00:00

    @Anil Nivargi - Welcome to Microsoft Q&A and thanks for reaching out to us.

    Also, thank you for providing the solution that worked for you as it can benefit the broader community here.

    So, rephrasing the solution here for broader audience:

    Using both winston-azure-application-insights and application insights can cause duplicate entries in Azure Application Insights as they serve the same functionality.

    Once you comment the code for winston-azure-application-insights, it should work as expected and you shouldn't see the duplicate entries in the application insights.

    in log.js, comment the 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;  
    

    Hope this helps. and please feel free to reach out if you have any further questions.

    ------------------------------------------------------------------

    If the above response was helpful, please feel free to "Accept as Answer" and "Upvote" the same so it can be beneficial to the community.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.