I am using the .net 4.5 framework. I am able to read the request to log in application insights using RequestTelemetry. Written the below code which is working.
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry == null) return;
var context = HttpContext.Current;
if (context == null) return;
if (context.Request != null)
{
if ((context.Request.HttpMethod == HttpMethod.Post.ToString()
|| context.Request.HttpMethod == HttpMethod.Put.ToString()) && WhitelistCheck(context.Request.RawUrl))
{
using (var reader = new StreamReader(context.Request.InputStream))
{
string requestPayload = reader.ReadToEnd();
if (!telemetry.Context.Properties.ContainsKey(Request_Payload))
{
// TO DO: Don't log Personally identifiable information (PII)
requestTelemetry.Properties.Add(Request_Payload, requestPayload);
}
}
}
}
To read the response, I am having the issue i e context.Response.OutputStream is write only, we can not read it directly. In core , we have response.body property but not in .net 4.5 framework. Written the below code to log i n application insights which are not working.
using (var reader = new StreamReader(context.Response.OutputStream))
{
string responseBody = reader.ReadToEnd();
if (!telemetry.Context.Properties.ContainsKey("Response"))
{
requestTelemetry.Properties.Add("Response", responseBody);
}
}
Please suggest