C# Application Insight 失败:TrackEvent 未发送到 Azure Application Insight

Yos*_*uya 3 azure azure-application-insights

我对 Azure Application Insight 完全陌生,并尝试通过我的本地计算机发送 TrackEvent。但Azure Application Insight似乎没有收到任何信息。

这是我的要求。规格 我安装了 application Insight sdk nuget 的示例 asp.net 框架控制台代码是使用遥测客户端和配置的 Instrumental Id 进行设置的。控制台运行后,Azure 应用程序洞察应按预期显示历史记录。但它没有显示任何东西。我错过了什么吗?

我的简单控制台代码

Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = "c453fec7-ea68-4c06-83e4-2938eef1f124";
        TelemetryClient telemetry = new TelemetryClient();
        telemetry.Context.User.Id = "Test User Id.";
        telemetry.Context.User.Id = "Test Device Id.";
        telemetry.TrackEvent("Test TrackEvent");
Run Code Online (Sandbox Code Playgroud)

InstrumentationKey 正确。Telemetry.TractEvent 方法不会公开异常。Azure Application Insight 搜索(针对自定义事件)不显示历史记录。

谢谢。

Iva*_*ang 5

原因可能是以下原因之一:

  1. 对于 .net 控制台应用程序,在门户中创建 Application Insights 时,应为应用程序类型选择“常规”。请参考下面的屏幕截图。 在此输入图像描述

2.在控制台代码中,您应该添加 Flush() 和 sleep() 方法。

        static void Main(string[] args)
        {
            TelemetryConfiguration.Active.InstrumentationKey = "your key";
            var telemetryClient = new TelemetryClient();
            telemetryClient.Context.User.Id = "uid1";
            telemetryClient.Context.Device.Id = "did1";
            telemetryClient.TrackEvent("AtestEvent7");
            telemetryClient.Flush();

            System.Threading.Thread.Sleep(5000);
        }
Run Code Online (Sandbox Code Playgroud)

然后稍等片刻,就可以在portal中看到自定义事件了: 在此输入图像描述

顺便说一句,我注意到您telemetry.Context.User.Id在代码中定义了两次,可能是拼写错误。