无法跟踪ApplicationInsights

时间:2017-08-22 07:25:50

标签: c# azure azure-application-insights

我已经创建了一个新的Azure Web作业,一个新的Application Insights资源,我只是想让一个人登录到另一个。据我所知,它应该像添加适当的NuGet包一样简单,并在这些行中添加一些代码:

TelemetryClient tc = new TelemetryClient();
tc.InstrumentationKey = "my-key";
tc.TrackEvent("testing");
tc.TrackTrace("test diag");
tc.Flush();
System.Threading.Thread.Sleep(1000);

睡眠是因为我找到了this MS文章,表明可能需要它。此代码发生在Web作业启动时(如此有效,它只是一个控制台应用程序)。但是,当我运行它时,我没有任何指标。

我已经尝试将密钥放在ApplicationInsights.config中,但这没有任何区别。我也尝试过不同类型的日志记录,包括异常。

我的猜测是上面的代码没有按照我的想法行事,但如果有人能指出我正确的方向,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

我可以通过在Azure Webjobs功能代码中插入TrackEvent调用来track user events,以下示例在我这方面工作得很好,你可以参考它。

<强> packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.ApplicationInsights" version="2.4.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.4.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.4.1" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.4.1" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.WindowsServer" version="2.4.1" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.4.0" targetFramework="net461" />
  <package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net461" />
  <package id="Microsoft.Azure.WebJobs" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Azure.WebJobs.Core" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Azure.WebJobs.Extensions" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Data.Edm" version="5.7.0" targetFramework="net461" />
  <package id="Microsoft.Data.OData" version="5.7.0" targetFramework="net461" />
  <package id="Microsoft.Data.Services.Client" version="5.7.0" targetFramework="net461" />
  <package id="Microsoft.Tpl.Dataflow" version="4.5.24" targetFramework="net461" />
  <package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.2.1" targetFramework="net461" />
  <package id="ncrontab" version="3.3.0" targetFramework="net461" />
  <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" />
  <package id="System.Diagnostics.DiagnosticSource" version="4.4.0" targetFramework="net461" />
  <package id="System.Spatial" version="5.7.0" targetFramework="net461" />
  <package id="WindowsAzure.Storage" version="7.2.1" targetFramework="net461" />
</packages>  

<强> Program.cs的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights;

namespace WebJob1
{
    // To learn more about Microsoft Azure WebJobs SDK, please see https://go.microsoft.com/fwlink/?LinkID=320976
    class Program
    {
        // Please set the following connection strings in app.config for this WebJob to run:
        // AzureWebJobsDashboard and AzureWebJobsStorage
        static void Main()
        {
            var config = new JobHostConfiguration();

            TelemetryConfiguration.Active.InstrumentationKey = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

            new TelemetryClient().TrackEvent("WebJobStart", new Dictionary<string, string> { { "appInsightsInstrumentationKey", TelemetryConfiguration.Active.InstrumentationKey } });

            config.DashboardConnectionString = "";
            config.UseTimers();


            var host = new JobHost(config);
            // The following code ensures that the WebJob will be running continuously
            host.RunAndBlock();
        }
    }
}

<强> Functions.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;

namespace WebJob1
{
    public class Functions
    {
        public static void TimerJob([TimerTrigger("00:01:00")] TimerInfo timerInfo, TextWriter log)
        {
            new TelemetryClient().TrackEvent("testing "+ DateTime.UtcNow.ToShortDateString(), new Dictionary<string, string> { { "appInsightsInstrumentationKey", TelemetryConfiguration.Active.InstrumentationKey } });

            log.WriteLine("Process Something called at : " + DateTime.Now.ToShortDateString());
        }
    }
}

Azure门户中的事件

enter image description here