Microsoft.ApplicationInsights.Web.RequestTrackingTelemetryModule.OnEndRequest C# (CSharp) Method

OnEndRequest() public method

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

            if (!this.NeedProcessRequest(context))
            {
                return;
            }

            var requestTelemetry = context.ReadOrCreateRequestTelemetryPrivate();
            requestTelemetry.Stop();

            // Success will be set in Sanitize on the base of ResponseCode
            if (string.IsNullOrEmpty(requestTelemetry.ResponseCode))
            {
                requestTelemetry.ResponseCode = context.Response.StatusCode.ToString(CultureInfo.InvariantCulture);
            }

            if (requestTelemetry.Url == null)
            {
                requestTelemetry.Url = context.Request.UnvalidatedGetUrl();
            }

            if (context.Request.Headers != null)
            {
                // If the source header is present on the incoming request, use that to populate the source field.
                string sourceIkey = context.Request.Headers[RequestResponseHeaders.SourceInstrumentationKeyHeader];

                if (!string.IsNullOrEmpty(sourceIkey))
                {
                    requestTelemetry.Source = sourceIkey;
                }
            }

            this.telemetryClient.TrackRequest(requestTelemetry);
        }

Usage Example

        public void OnEndCreatesRequestTelemetryIfBeginWasNotCalled()
        {
            var context = HttpModuleHelper.GetFakeHttpContext();

            var module = new RequestTrackingTelemetryModule();

            module.Initialize(TelemetryConfiguration.CreateDefault());
            module.OnEndRequest(context);

            Assert.NotNull(context.GetRequestTelemetry());
        }
All Usage Examples Of Microsoft.ApplicationInsights.Web.RequestTrackingTelemetryModule::OnEndRequest