The module subscribed to AppDomain.CurrentDomain.UnhandledException to send exceptions to ApplicationInsights.
Inheritance: ITelemetryModule, IDisposable
        public void DisposeCallsUnregister()
        {
            UnhandledExceptionEventHandler handler = null;
            using (var module = new UnhandledExceptionTelemetryModule(
                _ => { },
                h => handler = h,
                this.moduleChannel))
            {
            }

            Assert.NotNull(handler);
        }
        public void ModuleConstructorCallsRegister()
        {
            UnhandledExceptionEventHandler handler = null;

            using (var module = new UnhandledExceptionTelemetryModule(
                       h => handler = h,
                       _ => { },
                       this.moduleChannel))
            {
            }

            Assert.NotNull(handler);
        }
        public void EndpointAddressFromConfigurationActiveIsUsedForSending()
        {
            UnhandledExceptionEventHandler handler = null;
            using (var module = new UnhandledExceptionTelemetryModule(
                h => handler = h,
                _ => { },
                this.moduleChannel))
            {
                handler.Invoke(null, new UnhandledExceptionEventArgs(null, true));
            }

            Assert.Equal("http://test.com", this.moduleChannel.EndpointAddress);
        }
        public void DisposeCallsUnregister()
        {
            UnhandledExceptionEventHandler handler = null;

            using (var module = new UnhandledExceptionTelemetryModule(
                       _ => { },
                       h => handler = h,
                       this.moduleChannel))
            {
            }

            Assert.NotNull(handler);
        }
        public void DisposeDestructsChannel()
        {
            bool called = false;
            this.moduleChannel.OnDispose = () => called = true;

            using (var module = new UnhandledExceptionTelemetryModule(
                _ => { },
                _ => { },
                this.moduleChannel))
            {
            }

            Assert.True(called);
        }
        public void EndpointAddressFromConfigurationActiveIsUsedForSending()
        {
            UnhandledExceptionEventHandler handler = null;

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

            Assert.Equal("http://test.com", this.moduleChannel.EndpointAddress);
        }
        public void TrackedExceptionsHavePrefixUsedForTelemetry()
        {
            UnhandledExceptionEventHandler handler = null;

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

            Assert.True(this.items[0].Context.GetInternalContext().SdkVersion.StartsWith("unhnd: ", StringComparison.OrdinalIgnoreCase));
        }
        public void TrackedExceptionsHaveCorrectMessage()
        {
            UnhandledExceptionEventHandler handler = null;

            using (var module = new UnhandledExceptionTelemetryModule(
                       h => handler = h,
                       _ => { },
                       this.moduleChannel))
            {
                handler.Invoke(null, new UnhandledExceptionEventArgs(new ApplicationException("Test"), true));
            }

            Assert.Equal("Test", ((ExceptionTelemetry)this.items[0]).Exception.Message);
        }
        public void DisposeDestructsChannel()
        {
            bool called = false;

            this.moduleChannel.OnDispose = () => called = true;

            using (var module = new UnhandledExceptionTelemetryModule(
                       _ => { },
                       _ => { },
                       this.moduleChannel))
            {
            }

            Assert.True(called);
        }
        public void InstrumentationKeyCanBeOverridenInCodeAfterModuleIsCreated()
        {
            // This scenario is important for CloudApps where everything exept iKey comes from ai.config
            UnhandledExceptionEventHandler handler = null;

            using (var module = new UnhandledExceptionTelemetryModule(
                       h => handler = h,
                       _ => { },
                       this.moduleChannel))
            {
                TelemetryConfiguration.Active.InstrumentationKey = "MyKey2";
                handler.Invoke(null, new UnhandledExceptionEventArgs(null, true));
            }

            Assert.Equal("MyKey2", this.items[0].Context.InstrumentationKey);
        }
        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);
        }
        public void FlushIsCalledToBeSureDataIsSent()
        {
            bool called = false;

            this.moduleChannel.OnFlush = () => called = true;

            UnhandledExceptionEventHandler handler = null;

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

            Assert.True(called);
        }
        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);
        }
        public void FlushIsCalledToBeSureDataIsSent()
        {
            bool called = false;
            this.moduleChannel.OnFlush = () => called = true;

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

            Assert.True(called);
        }
        public void InstrumentationKeyCanBeOverridenInCodeAfterModuleIsCreated()
        {
            // This scenario is important for CloudApps where everything exept iKey comes from ai.config
            UnhandledExceptionEventHandler handler = null;
            using (var module = new UnhandledExceptionTelemetryModule(
                h => handler = h,
                _ => { },
                this.moduleChannel))
            {
                TelemetryConfiguration.Active.InstrumentationKey = "MyKey2";
                handler.Invoke(null, new UnhandledExceptionEventArgs(null, true));
            }

            Assert.Equal("MyKey2", this.items[0].Context.InstrumentationKey);
        }
        public void ModuleConstructorCallsRegister()
        {
            UnhandledExceptionEventHandler handler = null;
            using (var module = new UnhandledExceptionTelemetryModule(
                h => handler = h,
                _ => { },
                this.moduleChannel))
            {
            }

            Assert.NotNull(handler);
        }
        public void TrackedExceptionsHaveCorrectMessage()
        {
            UnhandledExceptionEventHandler handler = null;
            using (var module = new UnhandledExceptionTelemetryModule(
                h => handler = h,
                _ => { },
                this.moduleChannel))
            {
                handler.Invoke(null, new UnhandledExceptionEventArgs(new ApplicationException("Test"), true));
            }

            Assert.Equal("Test", ((ExceptionTelemetry)this.items[0]).Exception.Message);
        }
        public void TrackedExceptionsHavePrefixUsedForTelemetry()
        {
            UnhandledExceptionEventHandler handler = null;
            using (var module = new UnhandledExceptionTelemetryModule(
                h => handler = h,
                _ => { },
                this.moduleChannel))
            {
                handler.Invoke(null, new UnhandledExceptionEventArgs(null, true));
            }

            Assert.True(this.items[0].Context.GetInternalContext().SdkVersion.StartsWith("unhnd: ", StringComparison.OrdinalIgnoreCase));
        }