Application insights for a console application doesn't work
Asked Answered
L

2

6

I have created a basic console app with AI following the blog https://azure.microsoft.com/en-us/documentation/articles/app-insights-windows-desktop/, however no metrics are captured in AI of my Azure account. There isn't any error/exception reported either. Am I missing anything here ? Below is the code. There is some documentation that says HockeyApp is the new approach for capturing metrics for Windows Apps made available through app store, but there isn't enough information for console or windows services. Can you share your experience?

using System;
using Microsoft.ApplicationInsights;
namespace Logger
{
   class Program
   {
        static void Main(string[] args)
        {
            TelemetryClient tc = new TelemetryClient();
            tc.Context.User.Id = Environment.UserName;
            tc.Context.Session.Id = Guid.NewGuid().ToString();
            tc.Context.Device.OperatingSystem = Environment.OSVersion.ToString();
            tc.TrackPageView("Form1");
            tc.Flush();
            System.Threading.Thread.Sleep(1000);
        }
   }
}

ApplicationInsights.config

<applicationinsights xmlns="http://schemas.microsoft.com/A...">
 <instrumentationkey>
     mykey
 </instrumentationkey>
 <telemetrychannel type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel"/>
 <telemetryprocessors>
   <add type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor, Microsoft.AI.ServerTelemetryChannel">
     <maxtelemetryitemspersecond>5</maxtelemetryitemspersecond>
   </add>
 </telemetryprocessors>
 <telemetryinitializers>
    <add type="Microsoft.ApplicationInsights.WindowsServer.AzureRoleEnvironmentTelemetryInitializer, Microsoft.AI.WindowsServer"/>
    <add type="Microsoft.ApplicationInsights.WindowsServer.DomainNameRoleInstanceTelemetryInitializer, Microsoft.AI.WindowsServer"/>
    <add type="Microsoft.ApplicationInsights.WindowsServer.BuildInfoConfigComponentVersionTelemetryInitializer, Microsoft.AI.WindowsServer"/>
  </telemetryinitializers>
  <telemetrymodules>
    <add type="Microsoft.ApplicationInsights.WindowsServer.DeveloperModeWithDebuggerAttachedTelemetryModule, Microsoft.AI.WindowsServer"/>
    <add type="Microsoft.ApplicationInsights.WindowsServer.UnhandledExceptionTelemetryModule, Microsoft.AI.WindowsServer"/>
    <add type="Microsoft.ApplicationInsights.WindowsServer.UnobservedExceptionTelemetryModule, Microsoft.AI.WindowsServer"/>
  </telemetrymodules>
  <severity>Verbose</severity>
</applicationinsights>
Lugsail answered 10/7, 2016 at 21:53 Comment(0)
L
0

It appears the issue was with the Operating System (Windows 10 Pro). Upon copying it to Windows Server 2012 server, logs were appearing in AI. No clues as why it wasn't working in Windows 10 Pro. I have used LINQPad as suggested here and observed all traces were sent to AI properly but they were still not getting displayed in AI.

Lugsail answered 11/7, 2016 at 19:34 Comment(1)
Windows 10 Enterprise, Version 1703, Build 15063.786, it works for me. FYI. While it shouldn't matter, my code looks like your except I'm setting the InstrumentKey programatically. ex: /* tc = new TelementryClient */ tc.Context.InstrumentationKey = instrumentationKey; My packages were : <package id="Microsoft.ApplicationInsights" version="2.4.0" targetFramework="net452" /> <package id="Microsoft.ApplicationInsights.WindowsServer" version="2.4.1" targetFramework="net452" />Conferee
A
0

You need to add _ = TelemetryConfiguration.Active; as the first line in your Main method:

  static void Main(string[] args)
  {
    _ = TelemetryConfiguration.Active;

     var configuration = TelemetryConfiguration.CreateDefault();
     configuration.InstrumentationKey = "{Your Ai Instrumentation Key}";
     var tc = new TelemetryClient(configuration);
     tc.TrackTrace("Test");

     tc.Flush();
  }
Auspicious answered 11/10, 2019 at 12:26 Comment(1)
this is from 2016Charleycharlie

© 2022 - 2024 — McMap. All rights reserved.