C# 查看请求';应用程序洞察中的响应主体
是否可以在Application Insights中查看请求的响应正文? 我看过很多关于请求主体的问题/文章,但没有发现任何关于响应主体的问题/文章 我正在构建一个MVC core 2.1 Web Api 相关文章: 这是我的代码,从流创建读卡器时出现异常,即“流不可读。”C# 查看请求';应用程序洞察中的响应主体,c#,azure,azure-application-insights,telemetry,C#,Azure,Azure Application Insights,Telemetry,是否可以在Application Insights中查看请求的响应正文? 我看过很多关于请求主体的问题/文章,但没有发现任何关于响应主体的问题/文章 我正在构建一个MVC core 2.1 Web Api 相关文章: 这是我的代码,从流创建读卡器时出现异常,即“流不可读。” public class ResponseBodyInitializer : ITelemetryInitializer { readonly IHttpContextAccessor httpContextAcc
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;
}
}
}
}
}
}
------------------------------------更新----------------------
这是我记录请求和响应正文的完整代码。请求正文已正确记录(使用Request.EnableRewind();),但是,响应节在流读取器流不可读时引发异常
您必须实现
ITelemetryInitializer
。将IHttpContextAccessor
注入类,并在Initialize
方法中读取响应流。确保传递的ITelemetry
对象来自类型RequestTelemetry
,并且HttpRequest是Post或Put。然后,您可以使用IHttpContext.HttpContext.response.Body
属性读取响应,并使用Application Insight记录它
最后,在启动中的ConfigureService方法中注册类。cs我解决了上述问题,在下面的博客中解释了解决方案。基本上,它涉及使用消息处理程序将响应存储在请求消息中,并在实现ITelemetryInitialize的类中检索响应
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();
}
下面是一个详细解释的博客
我通过添加一个中间件解决了这个问题,该中间件将请求主体保存为
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
context.HttpContext?.Features.Get()?.Body
在阅读了ApplicationInsights github页面和AspNetCore github页面后,我得出了这个结论。是的,我确实尝试过,但在响应正文上创建流时出错:(。我按照链接,用异常信息和代码片段修改了问题。添加
context.Request.EnableRewind()
(是的,在请求对象上,而不是在响应上)。说明:我已经在问题的更新部分粘贴了我的完整代码,该部分还使用了Request.EnableRewind()但是仍然没有取得任何成果。嗨!你有解决方案吗?我遇到了完全相同的问题!这个问题是关于记录响应主体而不是请求主体的。谢谢@boylec1986,我当时没有意识到这一点。将答案留在这里,因为OP也更新了请求主体。