本文介绍了记录请求和对Application Insight的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将API请求有效负载和响应数据记录到Azure Application Insight.使用跟踪,我可以记录日志.但是我想知道什么是记录请求和响应数据以获取应用程序见解的最佳方法.由于数据量巨大,因此API调用数量将更多.我不能仅仅使用跟踪来跟踪成千上万的请求和响应数据.我尝试了一些博客,例如使用ITelemetryInitializer/httpcontext.feature,get,但没有运气.

I'm trying to log API request payload and response data to Azure Application Insight. Using trace I can able to log. but I want to know what is the best way to log request and response data to application insight. Because data is huge, no.of API calls will be more. I can't just trace hundreds of thousands of request and response data using tracing. I tried some of the blogs like using ITelemetryInitializer/ httpcontext.feature,get, but no luck.

我想从c#.Net框架,Web API,而不是.NET Core登录.

I want to log from c# .Net framework, Web API, not .NET Core.

我尝试过的示例代码.

public class AzureRequestResponseInitializer : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;

        if (requestTelemetry != null && (HttpContext.Current.Request.HttpMethod == HttpMethod.Post.ToString() || HttpContext.Current.Request.HttpMethod == HttpMethod.Get.ToString()))
        {
            using (var reader = new StreamReader(HttpContext.Current.Request.InputStream))
            {
                string requestBody = reader.ReadToEnd();
                requestTelemetry.Properties.Add("body", requestBody);
            }
        }

推荐答案

此问题在 https://thirum.wordpress.com/2019/08/19/logging-the-http-response-body-in-application-insights/

请看看.

这篇关于记录请求和对Application Insight的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:20
查看更多