Sonarr/src/NzbDrone.Common/TPL/TaskExtensions.cs

29 lines
847 B
C#
Raw Normal View History

2013-07-12 06:10:34 +00:00
using System.Threading.Tasks;
using NLog;
2013-08-31 01:42:30 +00:00
using NzbDrone.Common.Instrumentation;
2013-07-12 06:10:34 +00:00
namespace NzbDrone.Common.TPL
{
public static class TaskExtensions
{
2014-12-17 07:12:26 +00:00
private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(TaskExtensions));
2013-07-12 06:10:34 +00:00
public static Task LogExceptions(this Task task)
{
task.ContinueWith(t =>
{
2014-12-17 07:12:26 +00:00
if (t.Exception != null)
2013-07-12 06:10:34 +00:00
{
2014-12-17 07:12:26 +00:00
var aggregateException = t.Exception.Flatten();
foreach (var exception in aggregateException.InnerExceptions)
{
2016-02-11 21:13:42 +00:00
Logger.Error(exception, "Task Error");
2014-12-17 07:12:26 +00:00
}
2013-07-12 06:10:34 +00:00
}
}, TaskContinuationOptions.OnlyOnFaulted);
return task;
}
}
}