我批量向内部Api发送一堆请求,并具有以下(相当)标准代码:
var requests = new List<Task<HttpResponseMessage>>();
foreach (var req in requests)
{
requests.Add(client.SendAsync(req)); // client is HttpClient.
}
var results = await Task.WhenAll(requests);
以前,这些信息是一次发送的,而错误只是通过简单地检查响应并逐个抛出我自己的错误来解决的。
if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception($"Unexpected Status Code: {response.StatusCode}");
}
但是,我现在想做的是引发一个关于所有 not 返回200的请求的信息的异常。.信息不必过于广泛(目前) ),只有多少错误..如何定位错误的来源...
当前我在玩:
var e = new List<Exception>();
foreach (var result in results)
{
if (result.StatusCode != HttpStatuscode.OK)
{
var ex = new Exception($"Something helpful to locate batch.");
e.Add(ex);
}
}
throw new AggregateException(e);
是否可以将AggregateException
登录到Application Insights?也许是另一种方法。
int errorCount = 0;
foreach (var result in results)
{
if (result.StatusCode != HttpStatusCode.OK)
{
errorCount++;
}
}
// Assume some kind of way of identifying the batch of requests..
throw new Exception($"{errorCount} requests returned with unexepcted status code. Please check {batchIdentifier}");
这似乎有点胡言乱语,我确实认为这确实达到了我的目标……关于如何使它变得更加流畅的任何建议?