首页 > 解决方案 > 如何在应用洞察中编写 node js azure 函数日志?

问题描述

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

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

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

标签: node.jsloggingazure-functionsazure-web-app-service

解决方案


可以参考官方文档。有很多方法可以做到这一点。

在此处输入图像描述

官方文档中的示例代码。

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
});

推荐阅读