c# - 在 Application Insights 中查看 POST 请求正文

标签 c# asp.net-core-mvc azure-application-insights

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

我可以看到请求详细信息,但看不到应用程序洞察中发布的负载。我是否必须通过一些编码来跟踪它?

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

POST request

最佳答案

您可以简单地实现自己的 Telemetry Initializer :

例如,下面的实现提取有效负载并将其添加为请求遥测的自定义维度:

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

然后通过 configuration file 将其添加到配置中或通过代码:

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

然后在 Analytics 中查询:

requests | limit 1 | project customDimensions.body

关于c# - 在 Application Insights 中查看 POST 请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42686363/

相关文章:

asp.net - 拥有多个 IdentityUser 对象

asp.net-core - MVC 6 Web API : Resolving the location header on a 201 (Created)

azure - 应用洞察 : how can i get first 'details' column in the 'problemId' group

Azure Application Insights,如何通过 Azure CLI 更改每日上限

c# - Unity如何正确获取字符宽度

c# - 模拟 UserManager

asp.net-core-mvc - 使用 Controller 中的 session

logging - Azure Application Insights 查询自定义维度

c# - 如何从excel导入documentDB中的批量数据?

c# - 创建一个在列表更新时触发的事件