Initialize() public method

Initialize method is called after all configuration properties have been loaded from the configuration.
public Initialize ( Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration configuration ) : void
configuration Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration
return void
示例#1
0
        public PerformanceCounters(string instance)
        {
            try
            {
                var perfCollectorModule = new PerformanceCollectorModule();
                foreach (var p in typeof(PerformanceCounters).GetProperties())
                {
                    var counter = new PerformanceCounter(category, p.Name, instance, false);
                    p.SetValue(this, counter);
                    counter.RawValue = 0;

                    if (!p.Name.EndsWith("Base", StringComparison.Ordinal))
                    {
                        var perfCounterSpec = $"\\{category}({instance})\\{p.Name}";
                        var reportAs = p.Name
                            .Replace('_', ' ')
                            .Replace("Per", "/");

                        perfCollectorModule.Counters.Add(new PerformanceCounterCollectionRequest(perfCounterSpec, reportAs));
                    }
                }

                perfCollectorModule.Initialize(TelemetryConfiguration.Active);
            }
            catch (Exception e)
            {
                new TelemetryClient().TrackException(e);
            }
        }
        private static PerformanceCollectorModule InitializePerformanceCollectionModule()
        {
            var perfModule = new PerformanceCollectorModule();

            // we're running under IIS Express, so override the default behavior designed to prevent a deadlock
            perfModule.EnableIISExpressPerformanceCounters = true;

            // set test-friendly timings
            perfModule.CollectionPeriod = TimeSpan.FromMilliseconds(10);
            perfModule.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\Memory\Available Bytes", @"\Memory\Available Bytes"));
            perfModule.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"Will not parse;\Does\NotExist", @"Will not parse;\Does\NotExist"));

            perfModule.Counters.Add(new PerformanceCounterCollectionRequest(@"Will not parse", "Custom counter - will not parse"));

            perfModule.Counters.Add(new PerformanceCounterCollectionRequest(@"\Does\NotExist", "Custom counter - does not exist"));
            perfModule.Counters.Add(new PerformanceCounterCollectionRequest(@"\Process(??APP_WIN32_PROC??)\IDontExist", "Custom counter with placeholder - does not exist"));

            perfModule.Counters.Add(new PerformanceCounterCollectionRequest(@"\Process(??APP_WIN32_PROC??)\Handle Count", "Custom counter one"));

            perfModule.Counters.Add(
                new PerformanceCounterCollectionRequest(@"\ASP.NET Applications(??APP_W3SVC_PROC??)\Anonymous Requests/Sec", "Custom counter two"));

            perfModule.Initialize(TelemetryConfiguration.Active);
            return perfModule;
        }
        public static void ConfigureTelemetryModules(this TelemetryConfiguration configuration)
        {
            var performanceCounters = new PerformanceCollectorModule();
            performanceCounters.Counters.Add(new PerformanceCounterCollectionRequest(@"\Process\ID", "ID"));
            performanceCounters.Initialize(configuration);

            telemetryModules.Add(performanceCounters);

            var dependencies = new DependencyTrackingTelemetryModule();
            dependencies.Initialize(configuration);
           
            telemetryModules.Add(dependencies);
        }
        protected void Application_Start()
        {
            var setting = ConfigurationManager.AppSettings["TestApp.SendTelemetyIntemOnAppStart"];
            if (false == string.IsNullOrWhiteSpace(setting) && true == bool.Parse(setting))
            {
                new TelemetryClient().TrackTrace("Application_Start");
            }

            GlobalConfiguration.Configure(WebApiConfig.Register);

            var module = new PerformanceCollectorModule();

            // we're running under IIS Express, so override the default behavior designed to prevent a deadlock
            module.EnableIISExpressPerformanceCounters = true;

            // set test-friendly timings
            var privateObject = new PrivateObject(module);
            privateObject.SetField("collectionPeriod", TimeSpan.FromMilliseconds(10));
            privateObject.SetField(
                "defaultCounters",
                new List<string>() { @"\Memory\Available Bytes", @"Will not parse;\Does\NotExist" });

            module.Counters.Add(
                new PerformanceCounterCollectionRequest(@"Will not parse", "Custom counter - will not parse"));

            module.Counters.Add(
                new PerformanceCounterCollectionRequest(@"\Does\NotExist", "Custom counter - does not exist"));

            module.Counters.Add(
                new PerformanceCounterCollectionRequest(
                                          @"\Process(??APP_WIN32_PROC??)\Handle Count",
                    "Custom counter one"));

            module.Counters.Add(
                new PerformanceCounterCollectionRequest(
                                          @"\ASP.NET Applications(??APP_W3SVC_PROC??)\Anonymous Requests/Sec",
                    "Custom counter two"));

            // necessary for .NET CLR Memory counters to start reporting process ID
            GC.Collect();

            module.Initialize(TelemetryConfiguration.Active);

            TelemetryModules.Instance.Modules.Add(module);
        }
        public void TelemetryModuleIsNotInitializedTwiceToPreventTimerBeingRecreated()
        {
            var module = new PerformanceCollectorModule();
            PrivateObject privateObject = new PrivateObject(module);

            module.Initialize(TelemetryConfiguration.CreateDefault());
            object config1 = privateObject.GetField("telemetryConfiguration");

            module.Initialize(TelemetryConfiguration.CreateDefault());
            object config2 = privateObject.GetField("telemetryConfiguration");

            Assert.AreSame(config1, config2);
        }