Created
November 3, 2016 19:56
-
-
Save stuartleeks/620aa7858f54f864b27803febc176811 to your computer and use it in GitHub Desktop.
Application Insights - Capture Headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.ApplicationInsights.Channel; | |
using Microsoft.ApplicationInsights.Extensibility; | |
using System.Collections.Generic; | |
using System.Web; | |
namespace AppInsightsHeaders | |
{ | |
public class HeaderTelemetryInitializer : ITelemetryInitializer | |
{ | |
public List<string> RequestHeaders { get; set; } | |
public List<string> ResponseHeaders { get; set; } | |
public HeaderTelemetryInitializer() | |
{ | |
RequestHeaders = new List<string>(); | |
ResponseHeaders = new List<string>(); | |
} | |
public void Initialize(ITelemetry telemetry) | |
{ | |
var context = HttpContext.Current; | |
if (context == null) | |
{ | |
return; | |
} | |
if (context.Request != null) | |
{ | |
foreach (var headerName in RequestHeaders) | |
{ | |
var header = context.Request.Headers[headerName]; | |
if (header != null) | |
{ | |
telemetry.Context.Properties.Add($"request-{headerName}", header); | |
} | |
} | |
} | |
if (context.Response != null) | |
{ | |
foreach (var headerName in ResponseHeaders) | |
{ | |
var header = context.Response.Headers[headerName]; | |
if (header != null) | |
{ | |
telemetry.Context.Properties.Add($"response-{headerName}", header); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TelemetryConfiguration.Active.TelemetryInitializers.Add( | |
new HeaderTelemetryInitializer | |
{ | |
RequestHeaders = | |
{ | |
"Accept" | |
}, | |
ResponseHeaders = | |
{ | |
"Content-Type" | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment