/// <summary> /// Implements on begin callback of http module. /// </summary> public void OnBeginRequest(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 requestTelemetry = context.ReadOrCreateRequestTelemetryPrivate(); // NB! Whatever is saved in RequestTelemetry on Begin is not guaranteed to be sent because Begin may not be called; Keep it in context // In WCF there will be 2 Begins and 1 End. We need time from the first one if (requestTelemetry.Timestamp == DateTimeOffset.MinValue) { requestTelemetry.Start(); } }
/// <summary> /// Implements on end callback of http module. /// </summary> 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); }