locked
Application insights -read response body c# RRS feed

  • Question

  • User-1462918743 posted

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

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

    Streamreader is throwing exception, please suggest.

    Monday, February 11, 2019 6:30 AM

All replies

  • User61956409 posted

    Hi rameshgoudd@gmail.com,

    Welcome to ASP.NET forums.

    rameshgoudd@gmail.com

    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.

    Please clarify more about your scenario and requirement, and please tell us which event/method/handler you are using to execute the above code logic. 

    Besides, please check if you read/log the response after the request has been processed.

    With Regards,

    Fei Han

    Tuesday, February 12, 2019 2:42 AM
  • User-1462918743 posted

    Hi ,

    My requirement is to log the request and response in azure application insights.

    I am unable to read the response from HttpContext.Current object to log. I am writing the below code in Initialize.

    private static readonly string Request_Payload = "Request Payload";
     
        public void Initialize(ITelemetry telemetry)
        {
            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);
                        }
                    }
                }
            }
                //HttpResponseMessage response = context.Response;
                
                if (context.Response != null)
    {
                    //Can't read OutputStream
                    //if (context.Response.OutputStream.CanRead)
                    //{
                        using (var reader = new StreamReader(context.Response.OutputStream))
                        {
                            string responseBody = reader.ReadToEnd();
                            if (!telemetry.Context.Properties.ContainsKey("Response"))
                            {
                                requestTelemetry.Properties.Add("Response", responseBody);
                            }
                        }
                  //  }
     
                }
            }

    Tuesday, February 12, 2019 3:46 AM
  • User-1926401737 posted

    Hi,

    Do you collect both request and response details in your service side, not in a client application where you make request(s)? If possible, you can try to log reponse details in client application code logic after you made the request.

    Friday, February 15, 2019 3:06 AM