问题描述
JavaScript console object 提供了多种将消息输出到浏览器控制台的方法:
console.log
console.error
console.warn
console.info
console.trace
默认情况下,Application Insights 不会将控制台消息作为遥测发送到 Azure 门户 (Sending telemetry to the Azure portal),所以我的问题是:在 Application Insights 中捕获控制台消息作为遥测的方法是什么?
谢谢
解决方法
执行此操作的最佳方法可能是覆盖控制台功能本身。有一个现有的答案描述了如何做到这一点:Override console.log(); for production
请记住,对于 App Insights 的自定义实现,您通常需要使用 npm installation 而不是 App Insights 代码段来设置 App Insights。
结果可能如下所示:
// define a new console
var console=(function(oldCons){
return {
log: function(text){
oldCons.log(text);
telemetry.TrackTrace(text,SeverityLevel.Warning);
},//override other console methods
};
}(window.console));
//Then redefine the old console
window.console = console;