private AspNetDiagnosticTelemetryModule CreateModule(string rootIdHeaderName = null, string parentIdHeaderName = null)
        {
            var initializer = new Web.OperationCorrelationTelemetryInitializer();

            if (rootIdHeaderName != null)
            {
                initializer.RootOperationIdHeaderName = rootIdHeaderName;
            }

            if (parentIdHeaderName != null)
            {
                initializer.ParentOperationIdHeaderName = parentIdHeaderName;
            }

            this.configuration.TelemetryInitializers.Add(new Extensibility.OperationCorrelationTelemetryInitializer());

            AspNetDiagnosticTelemetryModule result = new AspNetDiagnosticTelemetryModule();

            var requestModule = new RequestTrackingTelemetryModule()
            {
                EnableChildRequestTrackingSuppression = false
            };

            var exceptionModule = new ExceptionTrackingTelemetryModule();

            requestModule.Initialize(this.configuration);
            exceptionModule.Initialize(this.configuration);

            TelemetryModules.Instance.Modules.Add(requestModule);
            TelemetryModules.Instance.Modules.Add(exceptionModule);

            result.Initialize(this.configuration);

            return(result);
        }
        public void WebApiExceptionLoggerIsInjectedAndTracksException()
        {
            Assert.IsFalse(GlobalConfiguration.Configuration.Services.GetServices(typeof(IExceptionLogger)).Any());

            using (var exceptionModule = new ExceptionTrackingTelemetryModule())
            {
                exceptionModule.Initialize(this.configuration);

                var webApiExceptionLoggers = GlobalConfiguration.Configuration.Services.GetServices(typeof(IExceptionLogger)).ToList();
                Assert.AreEqual(1, webApiExceptionLoggers.Count);

                var logger = (ExceptionLogger)webApiExceptionLoggers[0];
                Assert.IsNotNull(logger);

                var exception        = new Exception("test");
                var exceptionContext = new ExceptionLoggerContext(new ExceptionContext(exception, new ExceptionContextCatchBlock("catch block name", true, false)));
                logger.Log(exceptionContext);

                Assert.AreEqual(1, this.sentTelemetry.Count);

                var trackedException = (ExceptionTelemetry)this.sentTelemetry.Single();
                Assert.IsNotNull(trackedException);
                Assert.AreEqual(exception, trackedException.Exception);
            }
        }
        public void WebApiExceptionLoggerIsNotInjectedIfAnotherInjectionDetected()
        {
            GlobalConfiguration.Configuration.Services.Add(typeof(IExceptionLogger), new WebApiAutoInjectedLogger());
            Assert.AreEqual(1, GlobalConfiguration.Configuration.Services.GetServices(typeof(IExceptionLogger)).Count());

            using (var exceptionModule = new ExceptionTrackingTelemetryModule())
            {
                exceptionModule.Initialize(this.configuration);

                var loggers = GlobalConfiguration.Configuration.Services.GetServices(typeof(IExceptionLogger)).ToList();
                Assert.AreEqual(1, loggers.Count);
                Assert.IsInstanceOfType(loggers.Single(), typeof(WebApiAutoInjectedLogger));
            }
        }