来自 javascript 的 AppInsight TrackDependency() 不工作

AppInsight TrackDependency() from javascript is not working

我正在尝试登录到 appinsight 的依赖项 table,我正在使用前端代码 javascript。下面是片段。它执行得很好,但我没有看到依赖于 appinsight table 的任何条目。

var appInsights = window.appInsights || function (a){function b(a){c[a]=function(){var b=arguments;c.queue.push(function(){c[a].apply(c,b)})}}var c={config:a},d=document,e=window;setTimeout(function(){var b=d.createElement("script");b.src=a.url||"https://az416426.vo.msecnd.net/scripts/a/ai.0.js",d.getElementsByTagName("script")[0].parentNode.appendChild(b)});try{c.cookie=d.cookie}catch(a){}c.queue=[];for(var f=["Event","Exception","Metric","PageView","Trace","Dependency"];f.length;)b("track"+f.pop());if(b("setAuthenticatedUserContext"),b("clearAuthenticatedUserContext"),b("startTrackEvent"),b("stopTrackEvent"),b("startTrackPage"),b("stopTrackPage"),b("flush"),!a.disableExceptionTracking){f="onerror",b("_"+f);var g=e[f];e[f]=function(a,b,d,e,h){var i=g&&g(a,b,d,e,h);return!0!==i&&c["_"+f](a,b,d,e,h),i}}return c
     }({
         instrumentationKey: '@Model.InstrumentationKey'
     });



    window.appInsights = appInsights;
    appInsights.trackDependency({target:"http://dbname", name:"select customers proc", data:"SELECT * FROM Customers", duration:231, resultCode:0, success: true, dependencyTypeName: "ZSQL"});

上面的代码有什么问题吗?

应该是trackDependency方法使用不正确。 trackDependency的定义可以参考这个doc(在页面中搜索trackDependency).

这里是 another link 关于如何在 JavaScript 中使用 trackDependency 方法。

这是一个示例代码,在我这边工作(你可以修改它以满足你的需要):

<script type="text/javascript">
var appInsights=window.appInsights||function(config){
    function s(config) { t[config] = function () { var i = arguments; t.queue.push(function () { t[config].apply(t, i) }) } } var t = { config: config }, r = document, f = window, e = "script", o = r.createElement(e), i, u; for (o.src = config.url || "//az416426.vo.msecnd.net/scripts/a/ai.0.js", r.getElementsByTagName(e)[0].parentNode.appendChild(o), t.cookie = r.cookie, t.queue = [], i = ["Event", "Exception", "Metric", "PageView", "Trace","Dependency"];i.length;)s("track"+i.pop());return config.disableExceptionTracking||(i="onerror",s("_"+i),u=f[i],f[i]=function(config,r,f,e,o){var s=u&&u(config,r,f,e,o);return s!==!0&&t["_"+i](config,r,f,e,o),s}),t
}({
    instrumentationKey:"the key"
});
window.appInsights = appInsights;

// end of insertion
    appInsights.trackPageView();
    appInsights.trackTrace("sssssss114");
    appInsights.trackDependency("id", "GET", "https://www.bing.com", null, null, true, null, { prop1: 'abc' }, { meas1: 4.5 })
</script>

并且依赖数据显示在 Azure 门户 -> 应用程序洞察日志中: