C# 在Application Insights中记录失败请求的请求正文的最佳实践是什么?
当请求失败时,记录HTTP请求正文的最佳方式是什么 我通过重写异常记录器记录未处理的异常:C# 在Application Insights中记录失败请求的请求正文的最佳实践是什么?,c#,azure,asp.net-web-api,azure-application-insights,C#,Azure,Asp.net Web Api,Azure Application Insights,当请求失败时,记录HTTP请求正文的最佳方式是什么 我通过重写异常记录器记录未处理的异常: public class AiExceptionLogger : ExceptionLogger { public override void Log(ExceptionLoggerContext context) { if (context != null && context.Exception != null) {
public class AiExceptionLogger : ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
if (context != null && context.Exception != null)
{
ExceptionTelemetry telemetry = new ExceptionTelemetry(context.Exception);
// the requestBody is always empty because the stream is non-rewinadable?
string requestBody = context.Request.Content.ReadAsStringAsync().Result;
telemetry.Properties.Add("Request Body", requestBody);
Logger.LogException(telemetry);
}
base.Log(context);
}
}
使用上述代码,请求内容始终为空。我也尝试过,但由于调用GetBufferlessInputStream,因此引发了一个不受支持的方法异常。所以这也不起作用
我可以使用DelegatingHandler记录所有请求内容,但我只想记录由未处理异常导致的失败请求的请求正文
有什么想法吗?你说得对。不能以与流相同的方式重置Position属性。相反,复制内容并阅读副本
if (context != null && context.Exception != null)
{
HttpContent requestContent = new HttpContent();
request.Content.CopyToAsync(requestContent);
ExceptionTelemetry telemetry = new ExceptionTelemetry(context.Exception);
// the requestBody is always empty because the stream is non-rewinadable?
string requestBody = requestContent.ReadAsStringAsync().Result;
telemetry.Properties.Add("Request Body", requestBody);
Logger.LogException(context.Exception);
}
使用上述代码,请求内容始终为空
您可以使用ReadAsStreamAsync方法获取请求流,并重置此流的位置。之后,您可以使用StreamReader从这个steam中读取内容。以下代码仅供参考。我对它进行了测试,它在我这边运行得很好
ExceptionTelemetry telemetry = new ExceptionTelemetry(context.Exception);
//Get request stream and reset the position of this stream
Stream requestBodyStream = context.Request.Content.ReadAsStreamAsync().Result;
requestBodyStream.Position = 0;
string requestBody = string.Empty;
using (StreamReader sr = new StreamReader(requestBodyStream))
{
requestBody = sr.ReadToEnd();
}
telemetry.Properties.Add("Request Body", requestBody);
这里有一个替代Amor-MSFT的答案,使用
CopyTo
public class AiExceptionLogger : ExceptionLogger
{
public override async void Log(ExceptionLoggerContext context)
{
if (context != null && context.Exception != null)
{
ExceptionTelemetry telemetry = new ExceptionTelemetry(context.Exception);
using (var ms = new MemoryStream())
{
await context.Request.Content.CopyToAsync(ms);
var requestBody = Encoding.UTF8.GetString(ms.ToArray());
telemetry.Properties.Add("Request Body", requestBody);
}
Logger.LogException(telemetry);
}
base.Log(context);
}
}
HttpContent是一个抽象类,CopyTo也接收流:)您需要编辑答案。我会试试看它是否有效。我接受了你的答案,尽管我使用了米奇·斯图尔特的方法。但由于他的代码不完整,需要修改才能工作,我无法将其标记为正确答案:)@ErtayShashko我不确定为什么这是公认的答案,因为它不工作。您无法重置位置。@Phill,如果您无法重置环境中requestBodyStream的位置,您可以将其复制到MemoryStream并重置MemoryStream的位置。然后我们可以从MemoryStream读取数据。MemoryStream ms=新的MemoryStream();requestBodyStream.CopyTo(ms);ms.Position=0@Amor MSFT-它也不起作用。这不是我的环境。这是WebAPI2。无论如何,这个答案解决了我发布的代码在我的ASP.NETMVC项目中经过测试并运行良好的问题。感谢您指出ASP.NET Web API项目的解决方案。