本文介绍了在 Application Insights 中查看 POST 请求正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Application Insights 中查看 POST 请求正文?

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 track this with some coding?

我正在构建一个 MVC 核心 1.1 Web Api.

I am building a MVC core 1.1 Web Api.

推荐答案

您可以简单地实现自己的 遥测初始化器:

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

然后在 Analytics 中查询:

Then query it in Analytics:

requests | limit 1 | project customDimensions.body

这篇关于在 Application Insights 中查看 POST 请求正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 22:04
查看更多