我想在依赖关系表Application Insight中跟踪我的httpclient请求的响应正文。我的应用程序与.NET Framework 4.8一起运行我创建了一个初始化器,以使用以下代码来跟踪“依赖关系”遥测:
public class TrackResponseBody : ITelemetryInitializer { public void Initialize(ITelemetry telemetry) { var requestTelemetry = telemetry as DependencyTelemetry; if (requestTelemetry == null) return; if (requestTelemetry.TryGetOperationDetail("HttpResponse", out var responseObj)) { var response = responseObj as HttpWebResponse; if (response != null) { using (var stream = response.GetResponseStream()) { var reader = new StreamReader(stream); string result = reader.ReadToEnd(); requestTelemetry.Properties["ResponseBody"] = result; } } } } }
但是当我尝试调用
reader.ReadToEnd()
时,我的代码生成此异常:System.NotSupportedException: 'The stream does not support concurrent IO read or write operations.'
如果我不尝试获取正文响应,此代码将正确写入应用程序洞察依赖性日志中。
这就是我实现HttpClient
的方式:
的正文响应?]我想在依赖关系表Application Insight中跟踪我的httpclient请求的响应正文。我的应用程序在.NET Framework 4.8上运行。我创建了一个初始化程序来跟踪依赖项...var client_ = new HttpClient(); client_.BaseAddress = new Uri("https://www.google.com"); using (var request_ = new HttpRequestMessage()) { request_.Method = new HttpMethod("GET"); var response_ = await client_.SendAsync(request_).ConfigureAwait(false); var responseData_ = await response_.Content.ReadAsStringAsync().ConfigureAwait(false); }
是否可以获取我的[[HttpClient
TrackResponseBody
中,此行代码返回null:var response = responseObj as HttpWebResponse;
,response
变量始终为null。