如何配置 Application Insights 以从 .net 4.8 应用程序发送遥测数据

问题描述 投票:0回答:3

我似乎无法正确配置应用程序洞察。 也许我错过了一些非常重要的事情,或者它根本无法按照我的意图完成。 我有一个遗留问题

Windows Service project
,我正在尝试进行设置,向应用程序洞察发送一些遥测数据,获取一些异常记录和一些消息。

App Insight 已在 Azure 中为我设置,但在配置 C# 项目时没有收到任何异常或消息。它只是一个

Windows 服务项目

suedo 代码如下所示:

(别介意它连接在一起的奇怪方式。这只是为了让您了解当我有一些指标时我正在尝试重构什么)

static void Main(string[] args)
{

    var theService = new JobManagerService();
    theService.Start();
}
internal class JobManagerService
    {
        private readonly List<JobManager> _jobs = new List<JobManager>();
        private TelemetryClient _tc;

        public JobManagerService()
        {
            InitializeSomething();
        }

        public void Start()
        {
            var config = TelemetryConfiguration.CreateDefault();
            config.InstrumentationKey = ConfigurationManager.AppSettings["JobManagerInstrumentationKey"]; //IS BEING DEPRICATED
            config.ConnectionString = $"InstrumentationKey={ConfigurationManager.AppSettings["JobManagerInstrumentationKey"]};IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/;LiveEndpoint=https://westeurope.livediagnostics.monitor.azure.com/";
            config.TelemetryInitializers.Add(new AppInsightsJobManagerTelemetryInitializer());

            _tc = new TelemetryClient(config);
            _tc.TrackTrace($"Starting ApplicationInsights {nameof(TelemetryClient)} for {nameof(JobManagerService)}");
            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionEventRaised;

            _jobs.Add(new JobManager(interval, job, App_Online, _tc));
            _tc.TrackTrace($"Job {job.Name} Initialized with interval {interval} ms");
            _tc.Flush();
        }
        private void UnhandledExceptionEventRaised(object sender, UnhandledExceptionEventArgs e)
        {
            _tc.TrackException((Exception)e.ExceptionObject);
        }
        private class AppInsightsJobManagerTelemetryInitializer
        {
            public void Initialize(ITelemetry telemetry)
            {
                telemetry.Context.Properties["SystemGuid"] = Config.SystemConfiguration.SystemGuid.ToString();
                telemetry.Context.Properties["SystemName"] = Config.SystemConfiguration.SystemName.ToString();
                telemetry.Context.Properties["SystemType"] = Config.SystemConfiguration.SystemType.ToString();
            }
        }
    }
internal class JobManager
    {
        private readonly IJob _job;
        private readonly Timer _timer = new Timer();
        private readonly TelemetryClient _telemetryClient;

        public JobManager(int intInterval, IJob objJob, TelemetryClient telemetryClient)
        {
            if (intInterval > 60000) throw new ArgumentException("Interval cannot exceed 1 minute");
            //If intInterval > 60 * 1000 Then Throw New ArgumentException("Interval cannot exceed 1 minute")
            _telemetryClient = telemetryClient;

            _job = objJob;

            //' Load Jobs
            //_mObjJobCollection = new JobProviderService().ListByService(_job.Name);

            //' Starts the loop that triggers mObjTimer_Elapsed on every "interval"
            _timer.Interval = intInterval;
            _timer.Enabled = true;
            _timer.Elapsed += mObjTimer_Elapsed;

           
        }
        private void mObjTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                _timer.Enabled = false;
                var _mObjJobCollection = new JobProviderService().ListByService(_job.Name);

                foreach (var objJob in _mObjJobCollection)
                {
                    var objRunner = new JobRunner(objJob, _job);
                    objRunner.Run();
                }
            }
            catch (Exception ex)
            {
                _telemetryClient.TrackException(ex);
            }
            finally
            {
                _telemetryClient.TrackDependency("Windows Service", _job.Name, _job.Name);
            }
        }
    }

这个想法是一些作业每 1 分钟运行一次,我想捕获异常,然后发送一些日志消息。

但由于某种原因,我在在线应用程序洞察中没有得到 TrackException()TrackTrace() 仪器密钥和连接字符串是正确的,这应该足以让应用程序洞察建立连接,但没有显示任何内容。 我已经使用默认配置设置尝试了 applicationinsight.config 文件,但这也没有任何好处。

ApplicationInsights does break.
It just doesn't send data. Not when I debug locally, or when I push it to production.

我缺少什么,或者配置和使用遥测的正确方法应该是什么?

c# azure azure-application-insights .net-4.8
3个回答
1
投票

看来问题不是我想的那样。

配置本身是正确的,但客户端 Application Insights 未正确设置..

Appplication Insights SDK nuget 软件包有多种安装方式 主要是 “Microsoft.ApplicationInsights.Web”和“Microsoft.ApplicationInsights.WorkerService”

在我的例子中,Web 包已经安装,这使得我的 applicationInsights.config 文件变得混乱不堪。

删除 applicationInsights.config 文件,卸载 Application Insights nuget 包,然后重新安装“Microsoft.ApplicationInsights.WorkerService”,创建新的默认 applicationInsights.config 文件,并且开始传输数据。

这需要一些时间才能弄清楚。 但希望这能帮助未来的读者摆脱我的痛苦。

我很难从文档中弄清楚 Web/WorkerService 的区别。 大声疾呼: 托比亚斯·齐默格伦


0
投票

我至少可以想到一种可能导致该问题的情况。如果发生未处理的异常,telemetryClient 不会刷新,从而防止在应用程序终止之前发送遥测数据。

另外,请注意,如果您确实调用

Flush()
,则需要添加一点延迟,因为该过程是异步的:

_tc.Flush();
Thread.Sleep(2000);

或使用 FlushAsync:

await _tc.FlushAsync(CancellationToken.None);

请参阅此问题,了解需要使用

Thread.Sleep
与调用
FlushAsync

最后,确保在应用程序终止之前刷新所有路径上的遥测数据。鉴于所示的代码,很难判断情况是这样,特别是考虑到您的评论(不要介意它插入在一起的奇怪方式.. [..]


0
投票

此图显示了应用程序洞察的工作原理。摘自微软学习网站。

根据我的经验,这三个之一应该有效。

  1. 检查您是否能够在控制台中看到日志。
  2. 确保本地身份验证设置为启用。应用程序见解 >> 属性 >> 本地身份验证。

如果将其设置为禁用,您应该有权访问 Application Insights 和 Application Insights 工作区。 您可以在 Application Insights 的概览页面上看到工作区名称。

首先,您需要检查是否能够在控制台中看到遥测,如果是,问题可能出在门户上配置的 AppInsight。

  1. 尝试使用此 powershell 脚本向您的应用程序洞察发送可用性消息。

https://learn.microsoft.com/en-us/troubleshoot/azure/azure-monitor/app-insights/investigate-missing-telemetry#powershell-script-send-availability-test-result

希望这对您有用。

© www.soinside.com 2019 - 2024. All rights reserved.