我正在接收错误任务任务取消,没有任何内部异常细节,我也没有在Sentry中接收任务取消异常。如何查看此异常的堆栈跟踪或需要对代码进行哪些更改?
谢谢
private T CallDiffbotAndDeserialise<T>(string baseUrl, string pageUrl, int maxTags, int minimumTagConfidencePercentage)
{
var client = diffBotConnection.GetClient();
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
HttpResponseMessage response = client.GetAsync($"?token={settings.DiffBotToken}&maxTags={maxTags}&tagConfidence={minimumTagConfidencePercentage / 100}&url={Uri.EscapeDataString(pageUrl)}&ts={DateTime.Now.ToSafeCacheString()}").Result;
string responseString = response.Content.ReadAsStringAsync().Result;
T diffBotResponse = JsonConvert.DeserializeObject<T>(responseString);
return diffBotResponse;
}
catch (AggregateException e) // If the task is cancelled or times out
{
return default(T);
};
}API连接:
public abstract class APIConnection : IDisposable
{
protected HttpClient Client;
private bool disposed = false;
protected APIConnection() : this(3000) { }
protected APIConnection(int timeOut)
{
Client = new HttpClient()
{
Timeout = TimeSpan.FromMilliseconds(timeOut)
};
}
public HttpClient GetClient() => Client;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
Client.Dispose();
}
disposed = true;
}
}发布于 2018-07-28 09:10:23
您正在调用.Result,它总是抛出AggregateException。
这意味着您不仅要捕获TaskCancelledException或OperationCancelledException,还可以捕获对.Result的两个调用引发的任何内容。
由于您正在处理异常并隐藏它曾经发生过的事实(通过捕捉和返回),Sentry将不会知道它。如果要将该事件发送到Sentry,则需要手动调用Sentry客户端。
var ravenClient = new RavenClient("dsn"); // Initialize the client
ravenClient.CaptureEvent(new SentryEvent(exception));使用新SDK Sentry正在开发 (它仍然是预览版):
// Initialize the SDK only once, at the start of the app
using (SentrySdk.Init("dsn"))
{
SentrySdk.AddBreadcrumb($"Starting a web request to: {baseUrl}");
try
{
// make request
}
catch (Exception e)
{
SentrySdk.CaptureException(exception);
}
}在本例中,我添加了一个面包屑,在发生事件的情况下(例如,像上面这样显式捕获异常)将与事件一起发送。
还请注意,新的SDK会自动检测未处理的异常。这不是你的例子的情况,因为你是明确地抓住它。
我认为需要指出的是,理想情况下,可以通过调用.Result来避免阻塞线程,而应该使用async/await。
await关键字从错误的Task中释放Exception。这意味着您的catch块现在可以捕获OperationCancelledException。任何其他错误,如连接到服务器的完全失败,都不会进入catch块,而是会使堆栈冒泡。
https://stackoverflow.com/questions/51543538
复制相似问题