我似乎无法正确配置应用程序洞察。也许我错过了一些非常重要的事情,或者它只是不能做到,我的意图。我有一个遗留的Windows Service project
,我正在尝试设置它,向应用程序洞察发送一些遥测数据,获取一些异常日志和一些消息。
在Azure中已为我设置了App Insight,但在配置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分钟,我想捕捉异常,只是发送一些日志消息。
但是由于某些原因,我没有在app insights online中获得**TrackException()或TrackTrace()**Instrumentation键和Connectionstring是正确的,这应该足以让app insight建立连接,但什么都没有显示。我用applicationinsight.config文件进行了实验,使用了默认的配置设置,这也没有什么好处。
ApplicationInsights does break.
It just doesn't send data. Not when I debug locally, or when I push it to production.
我遗漏了什么,或者应该如何正确配置和使用遥测?
1条答案
按热度按时间n1bvdmb61#
我能想到至少有一种情况会导致这个问题。如果发生未处理的异常,则不会刷新telemetryClient,从而防止在应用程序终止之前发送遥测。
另外,请注意,如果您确实调用
Flush()
,则需要添加一点延迟,因为该进程是异步的:或者使用FlushAsync:
有关使用
Thread.Sleep
与调用FlushAsync
的需要,请参阅this issue。最后,确保在应用程序终止之前刷新所有路径上的遥测。鉴于所示的代码,很难判断是这种情况,特别是考虑到你的评论(* 别管它是如何插在一起的。[..]*)