Microsoft.ApplicationInsights.Web.ExceptionTrackingTelemetryModule.OnError C# (CSharp) Method

OnError() public method

Implements on error callback of http module.
public OnError ( HttpContext context ) : void
context System.Web.HttpContext
return void
        public void OnError(HttpContext context)
        {
            if (this.telemetryClient == null)
            {
                throw new InvalidOperationException("Initialize has not been called on this module yet.");
            }

            if (context == null)
            {
                WebEventSource.Log.NoHttpContextWarning();
                return;
            }

            var errors = context.AllErrors;

            if (errors != null && errors.Length > 0)
            {
                foreach (Exception exp in errors)
                {
                    var exceptionTelemetry = new ExceptionTelemetry(exp);

                    if (context.Response.StatusCode >= 500)
                    {
                        exceptionTelemetry.SeverityLevel = SeverityLevel.Critical;
                    }

                    this.telemetryClient.TrackException(exceptionTelemetry);
                }
            }
        }

Usage Example

        public void OnErrorDoesNotThrowOnNullContext()
        {
            var module = new ExceptionTrackingTelemetryModule();

            module.Initialize(this.configuration);
            module.OnError(null); // is not supposed to throw
        }
All Usage Examples Of Microsoft.ApplicationInsights.Web.ExceptionTrackingTelemetryModule::OnError
ExceptionTrackingTelemetryModule