I've setup an instance of INopStartup to add Application Insights telemetry to the application. Its super simple:


    public class PeakAppInsightsStartup : INopStartup
    {
        public int Order => 1;

        public void Configure(IApplicationBuilder application)
        {
        }

        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.AddApplicationInsightsTelemetry();
            services.AddSingleton<ITelemetryInitializer, SessionDetailsTelemetryEnrichment>();
        }
    }


Something I've learned over the years is to use a telemetry initializer so that the Application Map in app insights works well. Basically its just to set the cloud_RoleName value.  Then you get the nice pretty graphs representing all of your architecture and how they are connected.   Here is the code for that:


    public class SessionDetailsTelemetryEnrichment : TelemetryInitializerBase
    {
        private readonly IConfiguration _config;

        public SessionDetailsTelemetryEnrichment(IHttpContextAccessor httpContextAccessor, IConfiguration configuration) : base(httpContextAccessor)
        {
            _config = configuration;
        }

        protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
        {

            telemetry.Context.Cloud.RoleName = _config.GetValue<string>("ApplicationInsights:CloudRoleName");
        }
    }


However, that code isn't getting run for the scheduled tasks so they end up skewing my numbers. Does anyone know exactly how those run and if I have to inject my stuff into those separately?