/// <summary> /// Log this result object. This method is chainable. /// </summary> /// <param name="callerType">The type of the caller.</param> /// <param name="description"></param> /// <param name="message">If not specified, will log all messages in this result object.</param> /// <returns></returns> public Result WithLog(Type callerType, string description = null, string message = null) { if (this.Success) { if (message.HasValue()) { LoggingExtensions.LogForType(callerType).Info(message); } if (description.HasValue()) { LoggingExtensions.LogForType(callerType).Info(description); } if (!message.HasValue()) { foreach (string m in this.Messages) { LoggingExtensions.LogForType(callerType).Info(m); } } } else { if (message.HasValue()) { LoggingExtensions.LogForType(callerType).Error(message); } if (this.Code.HasValue() && description.HasValue()) { LoggingExtensions.LogForType(callerType).Error($"{this.Code} {description}"); } else if (this.Code.HasValue()) { LoggingExtensions.LogForType(callerType).Error(this.Code); } else if (description.HasValue()) { LoggingExtensions.LogForType(callerType).Error(description); } if (!message.HasValue()) { foreach (string m in this.Messages) { LoggingExtensions.LogForType(callerType).Error(m); } } if (this.Exception != null) { LoggingExtensions.LogForType(callerType).Error("Exception: ", this.Exception); } } return(this); }
/// <summary> /// Handle and log exceptions that occur in async tasks. /// </summary> /// <param name="task"></param> /// <param name="message"></param> /// <returns></returns> public static Task Log(this Task task, string message = "") { if (task.IsCompleted) { if (task.IsFaulted) { var ex = task.Exception; LoggingExtensions.Log <Task>().Error(message, ex); } return(task); } task.ContinueWith(t => { var ex = t.Exception; LoggingExtensions.Log <Task>().Error(message, ex); }, TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously); return(task); }