View POST request body in Application Insights

前端 未结 9 1577
抹茶落季
抹茶落季 2020-11-29 17:48

Is it possible to view POST request body in Application Insights?

I can see request details, but not the payload being posted in application insights. Do I have to t

9条回答
  •  死守一世寂寞
    2020-11-29 18:07

    You can simply implement your own Telemetry Initializer:

    For example, below an implementation that extracts the payload and adds it as a custom dimension of the request telemetry:

    public class RequestBodyInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            var requestTelemetry = telemetry as RequestTelemetry;
            if (requestTelemetry != null && (requestTelemetry.HttpMethod == HttpMethod.Post.ToString() || requestTelemetry.HttpMethod == HttpMethod.Put.ToString()))
            {
                using (var reader = new StreamReader(HttpContext.Current.Request.InputStream))
                {
                    string requestBody = reader.ReadToEnd();
                    requestTelemetry.Properties.Add("body", requestBody);
                }
            }
        }
    }
    

    Then add it to the configuration either by configuration file or via code:

    TelemetryConfiguration.Active.TelemetryInitializers.Add(new RequestBodyInitializer());
    

    Then query it in Analytics:

    requests | limit 1 | project customDimensions.body
    

提交回复
热议问题