public void TrackRespectsInstrumentaitonKeySetByTelemetryInitializer()
        {
            var sentTelemetry = new List <ITelemetry>();
            var channel       = new StubTelemetryChannel {
                OnSend = t => sentTelemetry.Add(t)
            };

            // No instrumentation key set here.
            var configuration = new TelemetryConfiguration(string.Empty, channel);

            var initializedTelemetry = new List <ITelemetry>();
            var telemetryInitializer = new StubTelemetryInitializer();

            telemetryInitializer.OnInitialize = item =>
            {
                item.Context.InstrumentationKey = "Foo";
                initializedTelemetry.Add(item);
            };

            configuration.TelemetryInitializers.Add(telemetryInitializer);

            var client = new TelemetryClient(configuration);

            var telemetry = new StubTelemetry();

            client.Track(telemetry);

            Assert.Equal(1, sentTelemetry.Count);
            Assert.Equal(1, initializedTelemetry.Count);
        }
Exemplo n.º 2
0
        public void TrackLogsDiagnosticsMessageOnExceptionsDuringTelemetryIntializersInitialize()
        {
            using (var listener = new TestEventListener())
            {
                listener.EnableEvents(CoreEventSource.Log, EventLevel.Error);

                var configuration = new TelemetryConfiguration {
                    InstrumentationKey = "Test key"
                };
                var telemetryInitializer = new StubTelemetryInitializer();
                var exceptionMessage     = "Test exception message";
                telemetryInitializer.OnInitialize = item => { throw new Exception(exceptionMessage); };
                configuration.TelemetryInitializers.Add(telemetryInitializer);

                var client = new TelemetryClient(configuration)
                {
                    Channel = new StubTelemetryChannel()
                };
                client.Track(new StubTelemetry());

                var exceptionExplanation = "Exception while initializing " + typeof(StubTelemetryInitializer).FullName;
                var diagnosticsMessage   = (string)listener.Messages.First().Payload[0];
                Assert.Contains(exceptionExplanation, diagnosticsMessage, StringComparison.OrdinalIgnoreCase);
                Assert.Contains(exceptionMessage, diagnosticsMessage, StringComparison.OrdinalIgnoreCase);
            }
        }
        public void TrackDoesNotThrowExceptionsDuringTelemetryIntializersInitialize()
        {
            var configuration        = new TelemetryConfiguration("Test key", new StubTelemetryChannel());
            var telemetryInitializer = new StubTelemetryInitializer();

            telemetryInitializer.OnInitialize = item => { throw new Exception(); };
            configuration.TelemetryInitializers.Add(telemetryInitializer);
            var client = new TelemetryClient(configuration);

            Assert.DoesNotThrow(() => client.Track(new StubTelemetry()));
        }
        public void TelemetryInitializersFromConfigurationActiveAreUsedForSending()
        {
            bool called = false;
            var  telemetryInitializer = new StubTelemetryInitializer {
                OnInitialize = item => called = true
            };

            TelemetryConfiguration.Active.TelemetryInitializers.Add(telemetryInitializer);

            UnhandledExceptionEventHandler handler = null;

            using (var module = new UnhandledExceptionTelemetryModule(
                       h => handler = h,
                       _ => { },
                       new InMemoryChannel()))
            {
                handler.Invoke(null, new UnhandledExceptionEventArgs(null, true));
            }

            Assert.True(called);
        }
Exemplo n.º 5
0
        public void TrackCopiesPropertiesFromClientToTelemetryBeforeInvokingInitializersBecauseExplicitlySetValuesTakePrecedence()
        {
            const string PropertyName = "TestProperty";

            string valueInInitializer = null;
            var    initializer        = new StubTelemetryInitializer();

            initializer.OnInitialize = telemetry => valueInInitializer = ((ISupportProperties)telemetry).Properties[PropertyName];

            var configuration = new TelemetryConfiguration {
                TelemetryChannel = new StubTelemetryChannel(), TelemetryInitializers = { initializer }
            };

            var client = new TelemetryClient(configuration);

            client.Context.Properties[PropertyName] = "ClientValue";
            client.Context.InstrumentationKey       = "Test Key";

            client.Track(new StubTelemetry());

            Assert.Equal(client.Context.Properties[PropertyName], valueInInitializer);
        }