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);
}