【问题标题】:How to write node js azure function logs in application insights?如何在应用洞察中编写节点 js 天蓝色函数日志?
【发布时间】:2020-11-20 07:34:06
【问题描述】:

我想在 node js azure 函数中执行日志记录,日志应该出现在应用程序洞察中。

我尝试使用 sdk,并且还检查了应用程序洞察力>实时指标>示例遥测,但是我使用 context.log() 添加的自定义日志没有出现在应用程序地图内的任何地方,而不是事件。

那么我还能在哪里查看日志?任何人都可以给我任何我可以参考的例子吗?

【问题讨论】:

  • 能否展示代码和相关设置文件?
  • const appInsights = require("applicationinsights"); appInsights.setup(config.APPINSIGHTS_INSTRUMENTATIONKEY) .setAutoCollectConsole(true, true) .setAutoCollectExceptions(true) .setAutoCollectPerformance(true, true) .start();常量客户端 = appInsights.defaultClient;在我的函数上方添加了这段代码
  • 在 catch 块中我添加了 client.trackException({exception: new Error(err), tagOverrides:operationIdOverride}); context.log.error('Error===================>',err) 但是当我从邮递员和研究应用洞察>实时指标>示例遥测

标签: node.js logging azure-functions azure-web-app-service


【解决方案1】:

您可以refer to the offical document。有很多方法可以做到。

官方文档中的示例代码。

let appInsights = require("applicationinsights");
appInsights.setup().start(); // assuming ikey in env var. start() can be omitted to 
disable any non-custom data
let client = appInsights.defaultClient;
client.trackEvent({name: "my custom event", properties: {customProperty: "custom 
property value"}});
client.trackException({exception: new Error("handled exceptions can be logged with this method")});
client.trackMetric({name: "custom metric", value: 3});
client.trackTrace({message: "trace message"});
client.trackDependency({target:"http://dbname", name:"select customers proc", 
data:"SELECT * FROM Customers", duration:231, resultCode:0, success: true, 
dependencyTypeName: "ZSQL"});
client.trackRequest({name:"GET /customers", url:"http://myserver/customers", duration:309, resultCode:200, success:true});

let http = require("http");
    http.createServer( (req, res) => {
    client.trackNodeHttpRequest({request: req, response: res}); // Place at the beginning of your request handler
});

【讨论】:

  • 谢谢你,Json,我会尝试使用这个实现,如果它对我有用,我会更新你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多