using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace System.Application { public static class TaskExtensions { public static Task Forget( this Task task, [CallerMemberName] string callerMemberName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = 0) { return task.ContinueWith( x => TaskLog.Raise(new TaskLog(callerMemberName, callerFilePath, callerLineNumber, x.Exception)), TaskContinuationOptions.OnlyOnFaulted); } public static void ForgetAndDispose( this Task task, [CallerMemberName] string callerMemberName = "", [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = 0) { task.ContinueWith( x => TaskLog.Raise(new TaskLog(callerMemberName, callerFilePath, callerLineNumber, x.Exception)), TaskContinuationOptions.OnlyOnFaulted).ContinueWith(s=>s.Dispose()); } public static Task WhenAll(this IEnumerable tasks) { return Task.WhenAll(tasks); } public static Task WhenAll(this IEnumerable> tasks) { return Task.WhenAll(tasks); } } public class TaskLog { public string CallerMemberName { get; } public string CallerFilePath { get; } public int CallerLineNumber { get; } public Exception Exception { get; } public TaskLog(string callerMemberName, string callerFilePath, int callerLineNumber, Exception exception) { this.CallerMemberName = callerMemberName; this.CallerFilePath = callerFilePath; this.CallerLineNumber = callerLineNumber; this.Exception = exception; } public static EventHandler Occured = (sender, e) => { const string format = @"Unhandled Exception occured from Task.Forget() ----------- Caller file : {1} : line {2} Caller member: {0} Exception: {3} "; System.Diagnostics.Debug.WriteLine(format, e.CallerMemberName, e.CallerFilePath, e.CallerLineNumber, e.Exception); }; internal static void Raise(TaskLog log) { Occured?.Invoke(typeof(TaskLog), log); } } }