【问题标题】:Unable to get ApplicationInsights to trace无法让 ApplicationInsights 进行跟踪
【发布时间】:2017-08-22 07:25:49
【问题描述】:

我创建了一个新的 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,但这并没有什么区别。我还尝试了不同类型的日志记录,包括异常。

我的猜测是上面的代码并没有像我想的那样做,但如果有人能指出正确的方向,我将不胜感激。

【问题讨论】:

  • 您是否直接检查事件/跟踪?它们可能被推送到 AppInsights,但没有直接处理。在统计数据出现在门户网站之前,我已经看到了超过 10 分钟的延迟。因此,稍后再查看可能是个好主意。
  • 是的 - 我已经把它放在了一夜之间。这取决于您所说的“直接检查”是什么意思 - 我正在使用 Analytics 通过门户网站检查它们。

标签: c# azure azure-application-insights


【解决方案1】:

我可以track user events在Azure Webjobs函数代码中插入TrackEvent调用,下面的示例在我这边运行良好,你可以参考一下。

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 门户中的事件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    相关资源
    最近更新 更多