Пример #1
0
        internal static Type[] GetTypesFromAssembly(
            Assembly assembly,
            IFullLogger log)
        {
            try
            {
                return(assembly.GetTypes());
            }
            catch (ReflectionTypeLoadException e)
            {
                // The array returned by the Types property of this exception contains a Type
                // object for each type that was loaded and null for each type that could not
                // be loaded, while the LoaderExceptions property contains an exception for
                // each type that could not be loaded.
                if (log != null)
                {
                    log.Warn(e, "Exception while detecting drawing types.");

                    foreach (var loaderException in e.LoaderExceptions)
                    {
                        log.Warn(loaderException, "Inner Exception for detecting drawing types.");
                    }
                }

                // null check here because mono doesn't appear to follow the MSDN documentation
                // as of July 2019.
                return(e.Types != null
                    ? e.Types.Where(x => x != null).ToArray()
                    : Array.Empty <Type>());
            }
        }
Пример #2
0
        /// <summary>
        /// Converts an observable into having exponential backoff retry support.
        /// </summary>
        /// <typeparam name="T">The type of the observable.</typeparam>
        /// <param name="source">The observable to add support to.</param>
        /// <param name="retryCount">The number of times to retry for.</param>
        /// <param name="strategy">The strategy to use for retrying. If null uses the DefaultStrategy.</param>
        /// <param name="retryOnError">Determines if a retry should be made based on the passed in exception. Defaults to retry on all exceptions.</param>
        /// <param name="scheduler">The scheduler to the output to.</param>
        /// <param name="log">The logger to output to.</param>
        /// <returns>The observable with exponential backoff retry support.</returns>
        public static IObservable <T> RetryWithBackoff <T>(
            this IObservable <T> source,
            int?retryCount = null,
            Func <int, TimeSpan> strategy       = null,
            Func <Exception, bool> retryOnError = null,
            IScheduler scheduler = null,
            IFullLogger?log      = default)
        {
            strategy ??= DefaultStrategy;
            scheduler ??= DefaultScheduler.Instance;
            retryOnError ??= _ => true;
            var attempt = 0;

            IObservable <Notification <T> > deferred = Observable.Defer(() =>
            {
                var num = attempt;
                attempt = num + 1;
                return((num == 0 ? source : source.DelaySubscription(strategy(attempt - 1), scheduler))
                       .Select(Notification.CreateOnNext)
                       .Catch((Func <Exception, IObservable <Notification <T> > >)
                                  (ex =>
                {
                    log?.Warn(ex, $"Retrying attempt: {attempt}");
                    return !retryOnError(ex)
                                ? Observable.Return(Notification.CreateOnError <T>(ex))
                                : Observable.Throw <Notification <T> >(ex);
                })));
            });

            return((retryCount is null ? deferred.Retry() : deferred.Retry(retryCount.Value)).Dematerialize());
        }
Пример #3
0
 /// <summary>
 /// Sends the value provided by the provided delegate, only if Warn is enabled.
 /// </summary>
 /// <typeparam name="T">The type of object we are logging about.</typeparam>
 /// <param name="logger">The logger to use.</param>
 /// <param name="function">The function to evaluate if Warn logging is enabled.</param>
 public static void Warn <T>(this IFullLogger logger, Func <string> function)
 {
     if (logger.IsWarnEnabled)
     {
         logger.Warn <T>(function.Invoke());
     }
 }
Пример #4
0
 /// <summary>
 /// Sends the value provided by the provided delegate, only if Warn is enabled.
 /// </summary>
 /// <param name="logger">The logger to use.</param>
 /// <param name="function">The function to evaluate if Warn logging is enabled.</param>
 /// <param name="exception">A exception to log about.</param>
 public static void WarnException(this IFullLogger logger, Func <string> function, Exception exception)
 {
     if (logger.IsWarnEnabled)
     {
         logger.Warn(function.Invoke(), exception);
     }
 }
Пример #5
0
        /// <summary>
        /// Sends the value provided by the provided delegate, only if Warn is enabled.
        /// </summary>
        /// <typeparam name="T">The type of object we are logging about.</typeparam>
        /// <param name="logger">The logger to use.</param>
        /// <param name="function">The function to evaluate if Warn logging is enabled.</param>
        public static void Warn <T>(this IFullLogger logger, Func <string> function)
        {
            if (logger is null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (function is null)
            {
                throw new ArgumentNullException(nameof(function));
            }

            if (logger.IsWarnEnabled)
            {
                logger.Warn <T>(function.Invoke());
            }
        }
Пример #6
0
        public static void LogForType(this IFullLogger logger, NotificationType notificationType, string message)
        {
            switch (notificationType)
            {
            case NotificationType.Info:
                logger.Info(message);
                break;

            case NotificationType.Warning:
                logger.Warn(message);
                break;

            case NotificationType.Error:
                logger.Error(message);
                break;
            }
        }