Files
Umbraco-CMS/src/Umbraco.Core/Logging/LoggingTaskExtension.cs

54 lines
2.5 KiB
C#
Raw Normal View History

2018-06-29 19:52:40 +02:00
using System;
using System.Threading;
2018-06-29 19:52:40 +02:00
using System.Threading.Tasks;
namespace Umbraco.Core.Logging
{
internal static class LoggingTaskExtension
{
/// <summary>
/// This task shouldn't be waited on (as it's not guaranteed to run), and you shouldn't wait on the parent task either (because it might throw an
/// exception that doesn't get handled). If you want to be waiting on something, use LogErrorsWaitable instead.
///
/// None of these methods are suitable for tasks that return a value. If you're wanting a result, you should probably be handling
/// errors yourself.
/// </summary>
public static Task LogErrors(this Task task, Action<string, Exception> logMethod)
{
return task.ContinueWith(
t => LogErrorsInner(t, logMethod),
CancellationToken.None,
TaskContinuationOptions.OnlyOnFaulted,
// Must explicitly specify this, see https://blog.stephencleary.com/2013/10/continuewith-is-dangerous-too.html
TaskScheduler.Default);
2018-06-29 19:52:40 +02:00
}
/// <summary>
/// This task can be waited on (as it's guaranteed to run), and you should wait on this rather than the parent task. Because it's
/// guaranteed to run, it may be slower than using LogErrors, and you should consider using that method if you don't want to wait.
///
/// None of these methods are suitable for tasks that return a value. If you're wanting a result, you should probably be handling
/// errors yourself.
/// </summary>
public static Task LogErrorsWaitable(this Task task, Action<string, Exception> logMethod)
{
return task.ContinueWith(
t => LogErrorsInner(t, logMethod),
// Must explicitly specify this, see https://blog.stephencleary.com/2013/10/continuewith-is-dangerous-too.html
TaskScheduler.Default);
2018-06-29 19:52:40 +02:00
}
private static void LogErrorsInner(Task task, Action<string, Exception> logAction)
{
if (task.Exception != null)
{
logAction("Aggregate Exception with " + task.Exception.InnerExceptions.Count + " inner exceptions: ", task.Exception);
foreach (var innerException in task.Exception.InnerExceptions)
{
logAction("Inner exception from aggregate exception: ", innerException);
}
}
}
}
}