Paul Michaels
Paul Michaels

Reputation: 16695

Unable to get ApplicationInsights to trace

I've created a new Azure Web job, a new Application Insights resource, and am simply trying to get one to log to the other. As I understand it, it should be as straight-forward as adding the appropriate NuGet packages, and adding some code along these lines:

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

The sleep is there because I found this MS article that suggested it might be needed. This code happens as the web job fires up (so effectively, it's just a console app). However, when I run it, I get no metrics whatsoever.

I've tried putting the key in the ApplicationInsights.config, but that makes no difference. I've also tried different types of logging, including exceptions.

My guess is that the code above isn't doing what I think it is, but I'd be grateful if someone could point me in the right direction as to why.

Upvotes: 1

Views: 226

Answers (1)

Fei Han
Fei Han

Reputation: 27793

I can track user events by inserting TrackEvent calls in Azure Webjobs function code, the following sample works fine on my side, you can refer to it.

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());
        }
    }
}

Events in the Azure portal

enter image description here

Upvotes: 1

Related Questions