Application Insights not logging custom events

 ̄綄美尐妖づ 提交于 2020-01-14 08:56:30

问题


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?


回答1:


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.



回答2:


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.



来源:https://stackoverflow.com/questions/37551596/application-insights-not-logging-custom-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!