如何获得 Application Insights 的正确 JavaScript 日志记录
How can I get proper JavaScript logging for Application Insights
我希望有人能回答为什么日志记录发生在一个位置而不是另一个位置的问题。我正在使用 snippet-based-setup 来启用日志记录,我有两个简单的测试日志,我输入了页面上的 JavaScript。
$(document).ready(function () {
RetrieveSessionData(function (d) {
formData = d;
var prop = { "SessionCode": getSessionCode() };
var met = { "model risk type": formData.projectionData.modelRiskType };
appInsights.trackEvent("ACTIONS-JS-READY", prop, met);
appInsights.trackEvent("ACTIONS-JS-READY2", "prop", "met");
appInsights.flush();
});
var properties = { "SessionCode": getSessionCode() };
var metrics = { "metrics field": 10 };
appInsights.trackEvent("ACTIONS-JS", properties, metrics);
appInsights.flush();
});
RetrieveSessionData 函数中的 appInsights.trackEvent 行不记录任何内容,它们也不 return 任何类型的 error/message 让我知道存在问题。脚本底部的那个,在函数之外,确实将自定义事件记录到应用程序洞察中。
我错过了什么?我还没有找到任何关于在函数内部进行 JavaScript 日志记录的正确方法的具体示例,还是不能这样做?
正如 OP 在评论中所说,Microsoft site 上的代码片段将起作用。
我不确定,但我在我身边测试过,它确实有效,这是我的代码。你检查过你在RetrieveSessionData
参数里的代码有没有执行?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
snippet-code-copy-from-tutorial-here
instrumentationKey: "ceec379c-xxxxx-4296327"
}});
</script>
<script src="../js/jquery-3.5.1.min.js"></script>
</head>
<body>
<div>this is page</div>
<script>
$(document).ready(function() {
console.log("============execute===============");
RetrieveSessionData(function (d) {
formData = d;
var prop = { "SessionCode": "aa" };
var met = { "model risk type": "bbb" };
appInsights.trackEvent("ACTIONS-JS-READY", prop, met);
appInsights.trackEvent("ACTIONS-JS-READY2", "prop", "met");
appInsights.flush();
});
console.log("============execute222===============");
var properties = {
"SessionCode": "session_code_01"
};
var metrics = {
"metrics field": 10
};
appInsights.trackEvent("ACTIONS-JS", properties, metrics);
appInsights.flush();
});
function RetrieveSessionData(func){
func();
}
</script>
</body>
</html>
我希望有人能回答为什么日志记录发生在一个位置而不是另一个位置的问题。我正在使用 snippet-based-setup 来启用日志记录,我有两个简单的测试日志,我输入了页面上的 JavaScript。
$(document).ready(function () {
RetrieveSessionData(function (d) {
formData = d;
var prop = { "SessionCode": getSessionCode() };
var met = { "model risk type": formData.projectionData.modelRiskType };
appInsights.trackEvent("ACTIONS-JS-READY", prop, met);
appInsights.trackEvent("ACTIONS-JS-READY2", "prop", "met");
appInsights.flush();
});
var properties = { "SessionCode": getSessionCode() };
var metrics = { "metrics field": 10 };
appInsights.trackEvent("ACTIONS-JS", properties, metrics);
appInsights.flush();
});
RetrieveSessionData 函数中的 appInsights.trackEvent 行不记录任何内容,它们也不 return 任何类型的 error/message 让我知道存在问题。脚本底部的那个,在函数之外,确实将自定义事件记录到应用程序洞察中。
我错过了什么?我还没有找到任何关于在函数内部进行 JavaScript 日志记录的正确方法的具体示例,还是不能这样做?
正如 OP 在评论中所说,Microsoft site 上的代码片段将起作用。
我不确定,但我在我身边测试过,它确实有效,这是我的代码。你检查过你在RetrieveSessionData
参数里的代码有没有执行?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
snippet-code-copy-from-tutorial-here
instrumentationKey: "ceec379c-xxxxx-4296327"
}});
</script>
<script src="../js/jquery-3.5.1.min.js"></script>
</head>
<body>
<div>this is page</div>
<script>
$(document).ready(function() {
console.log("============execute===============");
RetrieveSessionData(function (d) {
formData = d;
var prop = { "SessionCode": "aa" };
var met = { "model risk type": "bbb" };
appInsights.trackEvent("ACTIONS-JS-READY", prop, met);
appInsights.trackEvent("ACTIONS-JS-READY2", "prop", "met");
appInsights.flush();
});
console.log("============execute222===============");
var properties = {
"SessionCode": "session_code_01"
};
var metrics = {
"metrics field": 10
};
appInsights.trackEvent("ACTIONS-JS", properties, metrics);
appInsights.flush();
});
function RetrieveSessionData(func){
func();
}
</script>
</body>
</html>