Application Insights不记录自定义事件 [英] Application Insights not logging custom events

查看:69
本文介绍了Application Insights不记录自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#控制器的ASP.NET Core应用程序中设置了应用程序见解",它正在记录诸如页面浏览量",响应时间"之类的基本数据.但是我想创建一些自定义事件并将其也记录下来,但是我自定义事件无法显示在Azure门户中.目前,我正在使用免费版本的Application Insights.

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.

这很简单

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

其中的eventName是一个包含我要跟踪的自定义事件的字符串,而属性是一个字典,用于跟踪一些其他属性.

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.

运行该应用程序并几次击中这些行之后,我可以转到azure门户并查看基本信息,但是当我执行搜索"时,它表示有0个自定义事件并搜索任何按名称定义的自定义事件不会返回结果.

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

上面的事件有效,但是下面的事件无效,它根本没有记录该事件.这会在TelementryClient上调用TrackRequest以及TrackEvent,但我根本看不到这些.

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

我有点退缩了.我看到其中一些事件发生了,但是我背对背记录了很多事件,而我只看到了应该看到的6个事件中的2个?有什么想法怎么回事吗?

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?

推荐答案

Application Insights遥测客户端具有一个内存中缓冲区和刷新间隔(据我所记得,默认为1分钟),用于将缓冲的遥测发送到AI
您的Track方法具有遥测客户端的本地成员,该客户端在将数据实际刷新到AI端点之前已被垃圾收集".因此,您有三种选择(首先推荐):

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. 将遥测客户端存储为该类的成员,这将节省每次Track执行时的初始化,更重要的是-使客户端在刷新间隔内保持活动状态(只要您不重新生成ApplicationInsightsTracker每次).
  2. 调用后刷新内存缓冲区 通过调用Flush API,TrackEvent/TrackRequest/TrackX (appInsights.Flush()).
  3. 出于开发目的-您还可以启用开发人员模式,以便在每次跟踪的遥测中自动刷新缓冲区.
  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.

这篇关于Application Insights不记录自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆