Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Insights not logging custom events

I have setup Application Insights in my ASP.NET Core application in the C# Controller and it is logging basic data like Page Views, Response Time, etc. But I want to create some custom events and log those as well, but I cannot get any oft he Custom Events to show up in the Azure portal. Currently I'm using the Free version of Application Insights.

The is very straight forward

var appInsights = new TelemetryClient();
appInsights.TrackEvent(eventName, properties);

Where the eventName is a string containing the custom event that I want to track and properties is a Dictionary to track some additional properties.

After I run the app and hit those lines a couple of times I can then go to the azure portal and see the basic information, but when I do a Search it says that there is 0 Custom Events and searching for any of the custom events by name returns no results.

I have a class that has the Telemetry stuff in it below

public class ApplicationInsightsTracker : IApplicationTracker
{

    private readonly TelemetryClient AppInsights = new TelemetryClient();

    public void TrackEvent(string eventName, string userName, Dictionary<string, string> properties = null)
    {
        AppInsights.Context.User.AccountId = userName;
        TrackEvent(eventName, properties);
    }

    public void TrackRequest(string eventName, string userName, TimeSpan elapsed,
        Dictionary<string, string> properties = null)
    {
        AppInsights.Context.User.AccountId = userName;
        TrackEvent(eventName, properties);

        AppInsights.TrackRequest(eventName, DateTimeOffset.Now, elapsed, "200", true);
        AppInsights.Flush();
    }

    private void TrackEvent(string eventName, IDictionary<string, string> properties = null)
    {
        AppInsights.TrackEvent(eventName, properties);
        AppInsights.Flush();
    }

}

Then a simple example of me using it is

        Tracker.TrackEvent("Visit HomePage", User.Identity.Name);

Edit: The above event is working, but the below one is not, it is not logging this one at all. This calls the TrackRequest and also the TrackEvent on the TelementryClient, but I'm not seeing these at all.

    Tracker?.TrackRequest("Drill Report", userName, stopwatch.Elapsed, 
        new Dictionary<string, string>
        {
            { "DrillBy", report.DrillBy == null ? "None" : report.DrillBy.FriendlyName }
        });

I somewhat take that back. I am seeing some of these events come through, but I logged a bunch of them back to back and I only see 2 of the 6 that I should be seeing? Any ideas what could be going on?

like image 825
Paul Cavacas Avatar asked May 31 '16 16:05

Paul Cavacas


1 Answers

Application Insights telemetry client has an in-memory buffer and a flush interval (default of 1 minute, as far as I remember) for sending the buffered telemetry to AI endpoint.
Your Track methods have a local member of the telemetry client which is 'garbage collected' before it actually flushes the data to AI endpoint. Therefore, you have three options (recommended first):

  1. Store the telemetry client as a member of the class, which will spare the initialization on every Track execution and more important - will keep the client alive for the flush interval to kick-in (as long as you don't regenerate ApplicationInsightsTracker every time).
  2. Flush the in-memory buffer after calling TrackEvent/TrackRequest/TrackX, by calling the Flush API (appInsights.Flush()).
  3. For development purposes - you can also enable Developer Mode to automatically flush the buffer on each telemetry tracked.
like image 115
yonisha Avatar answered Sep 21 '22 16:09

yonisha