I seem to be unable to configure application insight correctly. Maybe I have missed something very important, or it just can't be done, the way I intent.
I have a legacy Windows Service project
, that I'm trying to get set up, to send some telemetry to app insight, to get some logging of exception, and some messages.
App Insight has been set up for me in Azure, but I don't get any exceptions, or messages when I configure my C# project. Its just a
Windows Service project
The suedo code looks like this:
(Nevermind the wierd way it is plugged together.. Thats just to give you an idea of what I'm trying to refactor, when I have some metrics)
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 DEPRECATED
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);
}
}
}
The idea is that some jobs get run every 1 minute, and I want to catch exceptions, and just send some log-messages.
But for some reason I don't get the TrackException()
or TrackTrace()
in app insights online
The Instrumenentation key and connection string are correct, which should be enough for app insight to establish a connection, but nothing shows up.
I've experimented with applicationinsight.config
file, using a default configuration setup, which doesn't do anything good either.
ApplicationInsights does break. It just doesn't send data. Not when I debug locally, or when I push it to production.
What am I missing, or what should be the correct way to configure, and use telemetry?
This diagram shows how application insights work. Taken from microsoft learn website.
Based on my experience one of this three should work.
If its set to disabled, you should have access to Application Insights and Application insights workspace. You can see workspace name on overview page of Application insights.
First you need to check if you are able to see Telemetry in console, if yes issue could be with AppInsight configured on portal.
Hope this works for you.