在Application Insights中查看请求的响应正文

时间:2018-08-03 11:00:34

标签: c# azure azure-application-insights telemetry

是否可以在Application Insights中查看请求的响应正文?  我已经看到了许多有关请求正文的问题/文章,但是没有发现有关响应正文的信息。

我正在构建MVC核心2.1 Web Api。

相关文章:

View POST request body in Application Insights

这是我的代码,从流创建读取器时出现异常,即“流不可读。”

public class ResponseBodyInitializer : ITelemetryInitializer
{
    readonly IHttpContextAccessor httpContextAccessor;

    public ResponseBodyInitializer(IHttpContextAccessor httpContextAccessor)
    {
        this.httpContextAccessor = httpContextAccessor;
    }

    public void Initialize(ITelemetry telemetry)
    {
        if (telemetry is RequestTelemetry requestTelemetry)
        {
            HttpContext httpContext = httpContextAccessor.HttpContext;
            HttpRequest request = httpContext.Request;
            HttpResponse response = httpContext.Response;

            if (request.Method == HttpMethods.Post || 
                 request.Method == HttpMethods.Put)
            {
                //Log the response body
                if (httpContext.Response.HasStarted)
                {
                    const string responseBody = "ResponseBody";

                    if (requestTelemetry.Properties.ContainsKey(responseBody))
                    {
                        return;
                    }

                    try
                    {
                        var stream = new StreamReader(response.Body);
                        var body = stream.ReadToEnd();                            
                        response.Body.Position = 0;
                        requestTelemetry.Properties.Add(responseBody, body);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }
    }
}  

---------------------------------- UPDATE --------- -----------------------

这是我记录请求和响应正文的完整代码。请求正文已正确记录(带有request.EnableRewind();),但是,响应部分在流读取器上抛出异常流不可读。

public class RequestBodyAndResponseBodyInitializer : ITelemetryInitializer
{
    readonly IHttpContextAccessor httpContextAccessor;

    public RequestBodyAndResponseBodyInitializer(IHttpContextAccessor httpContextAccessor)
    {
        this.httpContextAccessor = httpContextAccessor;
    }

    public void Initialize(ITelemetry telemetry)
    {
        if (telemetry is RequestTelemetry requestTelemetry)
        {
            HttpContext httpContext = httpContextAccessor.HttpContext;
            HttpRequest request = httpContext.Request;
            HttpResponse response = httpContext.Response;

            if (request.Method == HttpMethods.Post ||
                 request.Method == HttpMethods.Put)
            {
                //1- Log the request body
                if (request.Body.CanRead)
                {
                    const string requestBody = "RequestBody";
                    if (requestTelemetry.Properties.ContainsKey(requestBody))
                    {
                        return;
                    }

                    //Allows re-usage of the stream
                    request.EnableRewind();

                    var stream = new StreamReader(request.Body);
                    var body = stream.ReadToEnd();

                    //Reset the stream so data is not lost
                    request.Body.Position = 0;
                    requestTelemetry.Properties.Add(requestBody, body);
                }
                //2- Log the response body
                else if (httpContext.Response.HasStarted)
                {
                    //Allows re-usage of the stream
                    //request.EnableRewind();

                    const string responseBody = "ResponseBody";

                    if (requestTelemetry.Properties.ContainsKey(responseBody))
                    {
                        return;
                    }

                    try
                    {
                        //var stream = new StreamReader(response.Body);
                        //var body = stream.ReadToEnd();                            
                        //response.Body.Position = 0;
                        //requestTelemetry.Properties.Add(responseBody, body);

                        using (var memoryStream = new MemoryStream())
                        {
                            var stream = response.Body;
                            response.Body = memoryStream;

                            await next(context);

                            var body = new StreamReader(memoryStream).ReadToEnd();
                            //logger?.LogDebug($"Response: {responseBody}");

                            response.Body = stream;
                        }


                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                else { }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您必须实现ITelemetryInitializer。将IHttpContextAccessor注入到类中,并在Initialize方法中读取响应流。确保传递的ITelemetry对象来自类型RequestTelemetry,并且HttpRequest是Post还是Put。然后,您可以使用IHttpContext.HttpContext.Response.Body属性读取响应,并使用Application Insight记录响应。

最后,在Startup.cs的ConfigureService方法中注册您的类

答案 1 :(得分:0)

我通过添加一个中间件解决了它,该中间件将请求正文保存为HttpContext中的功能。重要的是,中间件必须在app.UseMvc()之前注册,因为Mvc框架会处理流。

Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.Use(async (httpContext, next) =>
        {
            var request = httpContext.Request;

            if (request.Method == HttpMethods.Post || request.Method == HttpMethods.Put && request.Body.CanRead)
            {
                //Allows re-usage of the stream
                request.EnableBuffering();
                request.Body.Seek(0, SeekOrigin.Begin);

                using (var stream = new StreamReader(request.Body, Encoding.UTF8, true, 1024, true))
                {
                    var body = await stream.ReadToEndAsync();
                    //Reset the stream so data is not lost
                    request.Body.Position = 0;
                    var bodyFeature = new RequestBodyFeature(body); // RequestBodyFeature is a simple POCO
                    httpContext.Features.Set(bodyFeature);
                }

                request.Body.Seek(0, SeekOrigin.Begin);
            }

            await next.Invoke();
        });

        app.UseMvc();
}

当我从ExceptionFilter登录到ApplicationInsights时,我得到的是这样的主体:

context.HttpContext?.Features.Get<RequestBodyFeature>()?.Body

在阅读ApplicationInsights github页面上的this issue和AspNetCore github页面上的this issue之后,我得出了这个结论。

答案 2 :(得分:-2)

我解决了以上问题,在以下博客中解释了该解决方案。基本上,它涉及使用消息处理程序将响应存储在请求消息中,并在实现ITelemetryInitialize的类中检索响应。

这是详细解释博客。

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