Telemetry module to collect unhandled exceptions caught by http module.
Inheritance: ITelemetryModule, IDisposable
        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationInsightsHttpModule"/> class.
        /// </summary>
        public ApplicationInsightsHttpModule()
        {
            try
            {
                // The call initializes TelemetryConfiguration that will create and Intialize modules
                TelemetryConfiguration configuration = TelemetryConfiguration.Active;

                foreach (var module in TelemetryModules.Instance.Modules)
                {
                    if (module is RequestTrackingTelemetryModule)
                    {
                        this.requestModule = (RequestTrackingTelemetryModule)module;
                    }
                    else
                    {
                        if (module is ExceptionTrackingTelemetryModule)
                        {
                            this.exceptionModule = (ExceptionTrackingTelemetryModule)module;
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                this.isEnabled = false;
                WebEventSource.Log.WebModuleInitializationExceptionEvent(exc.ToInvariantString());
            }
        }
        public void OnErrorDoesNotThrowOnNullContext()
        {
            var module = new ExceptionTrackingTelemetryModule();

            module.Initialize(this.configuration);
            module.OnError(null); // is not supposed to throw
        }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ApplicationInsightsHttpModule"/> class.
 /// </summary>
 public ApplicationInsightsHttpModule()
 {
     try
     {
         // The call initializes TelemetryConfiguration that will create and Intialize modules
         TelemetryConfiguration configuration = TelemetryConfiguration.Active;
         foreach (var module in TelemetryModules.Instance.Modules)
         {
             if (module is RequestTrackingTelemetryModule)
             {
                 this.requestModule = (RequestTrackingTelemetryModule)module;
             }
             else
             {
                 if (module is ExceptionTrackingTelemetryModule)
                 {
                     this.exceptionModule = (ExceptionTrackingTelemetryModule)module;
                 }
             }
         }
     }
     catch (Exception exc)
     {
         this.isEnabled = false;
         WebEventSource.Log.WebModuleInitializationExceptionEvent(exc.ToInvariantString());
     }
 }
        public void OnErrorDoesNotThrowOnNullContext()
        {
            var module = new ExceptionTrackingTelemetryModule();

            module.Initialize(this.configuration);
            module.OnError(null); // is not supposed to throw
        }
        public void MvcExceptionFilterIsNotInjectedIsInjectionIsDisabled()
        {
            using (var exceptionModule = new ExceptionTrackingTelemetryModule())
            {
                exceptionModule.EnableMvcAndWebApiExceptionAutoTracking = false;
                exceptionModule.Initialize(this.configuration);

                Assert.IsFalse(GlobalFilters.Filters.Any());
            }
        }
コード例 #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationInsightsHttpModule"/> class.
        /// </summary>
        public ApplicationInsightsHttpModule()
        {
            try
            {
                // The call initializes TelemetryConfiguration that will create and Intialize modules
                TelemetryConfiguration configuration = TelemetryConfiguration.Active;
                foreach (var module in TelemetryModules.Instance.Modules)
                {
                    if (module is RequestTrackingTelemetryModule)
                    {
                        this.requestModule = (RequestTrackingTelemetryModule)module;
                    }
                    else
                    {
                        if (module is ExceptionTrackingTelemetryModule)
                        {
                            this.exceptionModule = (ExceptionTrackingTelemetryModule)module;
                        }
                    }
                }

                // We use reflection here because 'AddOnSendingHeaders' is only available post .net framework 4.5.2. Hence we call it if we can find it.
                // Not using reflection would result in MissingMethodException when 4.5 or 4.5.1 is present.
                this.addOnSendingHeadersMethod       = typeof(HttpResponse).GetMethod("AddOnSendingHeaders");
                this.addOnSendingHeadersMethodExists = this.addOnSendingHeadersMethod != null;

                if (this.addOnSendingHeadersMethodExists)
                {
                    this.addOnSendingHeadersMethodParam = new Action <HttpContext>((httpContext) =>
                    {
                        try
                        {
                            if (this.requestModule != null)
                            {
                                this.requestModule.AddTargetHashForResponseHeader(httpContext);
                            }
                        }
                        catch (Exception ex)
                        {
                            WebEventSource.Log.AddTargetHeaderFailedWarning(ex.ToInvariantString());
                        }
                    });
                    this.addOnSendingHeadersMethodParams = new object[] { this.addOnSendingHeadersMethodParam };
#if !NET40
                    this.openDelegateForInvokingAddOnSendingHeadersMethod = this.CreateOpenDelegate(this.addOnSendingHeadersMethod);
#endif
                }
            }
            catch (Exception exc)
            {
                this.isEnabled = false;
                WebEventSource.Log.WebModuleInitializationExceptionEvent(exc.ToInvariantString());
            }
        }
        public void OnErrorSetsSeverityToCriticalForRequestWithStatusCode500()
        {
            var platformContext = HttpModuleHelper.GetFakeHttpContext();
            platformContext.Response.StatusCode = 500;
            platformContext.AddError(new Exception());

            var module = new ExceptionTrackingTelemetryModule();
            module.Initialize(this.configuration);
            module.OnError(platformContext);

            Assert.Equal(SeverityLevel.Critical, ((ExceptionTelemetry)this.sendItems[0]).SeverityLevel);
        }
        public void OnErrorSetsSeverityToCriticalForRequestWithStatusCode500()
        {
            var platformContext = HttpModuleHelper.GetFakeHttpContext();

            platformContext.Response.StatusCode = 500;
            platformContext.AddError(new Exception());

            var module = new ExceptionTrackingTelemetryModule();

            module.Initialize(this.configuration);
            module.OnError(platformContext);

            Assert.Equal(SeverityLevel.Critical, ((ExceptionTelemetry)this.sendItems[0]).SeverityLevel);
        }
        public void MvcExceptionLoggerIsNotInjectedIfAnotherInjectionDetected()
        {
            GlobalFilters.Filters.Add(new MvcAutoInjectedFilter());
            Assert.AreEqual(1, GlobalFilters.Filters.Count);

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

                var filters = GlobalFilters.Filters;
                Assert.AreEqual(1, filters.Count);
                Assert.IsInstanceOfType(filters.Single().Instance, typeof(MvcAutoInjectedFilter));
            }
        }
        public void OnErrorTracksExceptionsThatArePresentInHttpContext()
        {
            var platformContext = HttpModuleHelper.GetFakeHttpContext();
            var exception1 = new Exception("1");
            platformContext.AddError(exception1);
            platformContext.AddError(new Exception("2"));

            var module = new ExceptionTrackingTelemetryModule();
            module.Initialize(this.configuration);
            module.OnError(platformContext);

            Assert.Equal(2, this.sendItems.Count);
            Assert.Equal(exception1, ((ExceptionTelemetry)this.sendItems[0]).Exception);
        }
        public void OnErrorTracksExceptionsThatArePresentInHttpContext()
        {
            var platformContext = HttpModuleHelper.GetFakeHttpContext();
            var exception1      = new Exception("1");

            platformContext.AddError(exception1);
            platformContext.AddError(new Exception("2"));

            var module = new ExceptionTrackingTelemetryModule();

            module.Initialize(this.configuration);
            module.OnError(platformContext);

            Assert.Equal(2, this.sendItems.Count);
            Assert.Equal(exception1, ((ExceptionTelemetry)this.sendItems[0]).Exception);
        }
        public void MvcExceptionFilterNoopIfCustomErrorsIsFalse()
        {
            using (var exceptionModule = new ExceptionTrackingTelemetryModule())
            {
                exceptionModule.Initialize(this.configuration);

                var mvcExceptionFilters = GlobalFilters.Filters;
                Assert.AreEqual(1, mvcExceptionFilters.Count);

                var handleExceptionFilter = (HandleErrorAttribute)mvcExceptionFilters.Single().Instance;
                Assert.IsNotNull(handleExceptionFilter);

                var exception     = new Exception("test");
                var controllerCtx = HttpModuleHelper.GetFakeControllerContext(isCustomErrorEnabled: false);
                handleExceptionFilter.OnException(new ExceptionContext(controllerCtx, exception));

                Assert.IsFalse(this.sentTelemetry.Any());
            }
        }
        /// <summary>
        /// Initializes the telemetry module.
        /// </summary>
        /// <param name="configuration">Telemetry configuration to use for initialization.</param>
        public void Initialize(TelemetryConfiguration configuration)
        {
            try
            {
                foreach (var module in TelemetryModules.Instance.Modules)
                {
                    if (module is RequestTrackingTelemetryModule requestTrackingModule)
                    {
                        this.requestModule = requestTrackingModule;
                    }
                    else if (module is ExceptionTrackingTelemetryModule exceptionTracingModule)
                    {
                        this.exceptionModule = exceptionTracingModule;
                    }
                }
            }
            catch (Exception exc)
            {
                this.isEnabled = false;
                WebEventSource.Log.WebModuleInitializationExceptionEvent(exc.ToInvariantString());
            }

            this.allListenerSubscription = DiagnosticListener.AllListeners.Subscribe(this);
        }
 public AspNetEventObserver(RequestTrackingTelemetryModule requestModule, ExceptionTrackingTelemetryModule exceptionModule)
 {
     this.requestModule   = requestModule;
     this.exceptionModule = exceptionModule;
 }