8
votes

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?

3
can you show an exact example? how are you searching by name? if you can see them in the search view with no filters, then you should be able to search for them as well. you might have a subtle issue with the exact syntax of what you are typing into search in the portal?John Gardner
I cannot see them at all. When I click search the tile that says Custom Event says 0 and I can't find them at all. As far as an exact example. See code abovePaul Cavacas
when you debug your application, are you seeing lines like: "Application Insights Telemetry: {something here|}" in the debug output window? i want to make sure everything is actually getting out. Or, if you use fiddler, can you see outbound requests to "dc.servies.visualstudio.com" going out from your app? are they successful? (200s?)John Gardner
if your data is going out successfully, and to the expected instrumentation key, it might also be that the backend is delayed. usually events should show up in the portal within a couple minutes, but you might want to check aka.ms/aistatus to see the current status, just to make sure (i don't see anything there that would imply the backend is behind)John Gardner
(also: nit: you shouldn't need an custom "occurred" property, event time is a standard thing that's already on all of the events). you might also want to put the username field into the built in telemetry.context.user.id field (see stackoverflow.com/questions/30434924/…)John Gardner

3 Answers

9
votes

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.
0
votes

I suspect that some essential configuration was not initialized when you constructed TelemetryClient() object. It might be something easy like "no instrumentation key" in Telemetry Client object, or something more hidden that's read from TelemetryConfiguration() object.

You might want to check outgoing HTTP traffic for failed requests to dc.services.visualstudio.com - the error might give a clue on what to fix/initialize.

Also, you can take a look at the getting started specifically for Asp.Net core projects - it might contain the missing piece you are looking for.

The code of AI WEB SDK and AI ASP.NET core SDK is on GitHub, so you can quickly navigate through code to see what else can go sidetrack here.

0
votes

I had similar issue. I was creating a telemetry like this:

var tc = new Microsoft.ApplicationInsights.TelemetryClient(
                new TelemetryConfiguration({your intrumentation key})
                );
            tc.TrackEvent("CreatedAccount", new Dictionary<string, string> { { "AccountType", "My action" } }, null);
            tc.TrackEvent("AddedItemToCart", new Dictionary<string, string> { { "Item", "my item" } }, null);
            tc.TrackEvent("CompletedPurchase");

            tc.Flush();

As soon as I change it to do like this it started to work and I was able to see the events in the search for customEvents in application insights:

 var tc = new Microsoft.ApplicationInsights.TelemetryClient(
                new TelemetryConfiguration()
                { ConnectionString = "InstrumentationKey={your key}" }
                );
            tc.TrackEvent("CreatedAccount", new Dictionary<string, string> { { "AccountType", "My action" } }, null);
            tc.TrackEvent("AddedItemToCart", new Dictionary<string, string> { { "Item", "my item" } }, null);
            tc.TrackEvent("CompletedPurchase");

            tc.Flush();