コード例 #1
0
        /// <summary>
        /// Encloses an action in try catch and logs the error if it leaked from action. This method never leaks.
        /// Returns true if there was no error on action success, or false if error leaked from action and was logged by component.
        /// The actual logging depends on the component log level
        /// </summary>
        public static TResult DontLeak <TResult>(this IApplicationComponent cmp, Func <TResult> func, string errorText = null, [CallerMemberName] string errorFrom = null, Log.MessageType errorLogType = Log.MessageType.Error)
        {
            var ac = (cmp.NonNull(nameof(cmp)) as ApplicationComponent).NonNull("Internal error: not a AC");

            func.NonNull(nameof(func));
            try
            {
                return(func());
            }
            catch (Exception error)
            {
                if (errorText.IsNullOrWhiteSpace())
                {
                    errorText = "Error leaked: ";
                }
                errorText += error.ToMessageWithType();

                ac.WriteLog(errorLogType, errorFrom, errorText, error);
            }

            return(default(TResult));
        }
コード例 #2
0
        /// <summary>
        /// Encloses an action in try catch and logs the error if it leaked from action. This method never leaks.
        /// Returns true if there was no error on action success, or false if error leaked from action and was logged by component.
        /// The actual logging depends on the component log level
        /// </summary>
        public static bool DontLeak(this IApplicationComponent cmp, Action action, string errorText = null, [CallerMemberName] string errorFrom = null, Log.MessageType errorLogType = Log.MessageType.Error)
        {
            var ac = (cmp.NonNull(nameof(cmp)) as ApplicationComponent).NonNull("Internal error: not a AC");

            action.NonNull(nameof(action));
            try
            {
                action();
                return(true);
            }
            catch (Exception error)
            {
                if (errorText.IsNullOrWhiteSpace())
                {
                    errorText = "Error leaked: ";
                }
                errorText += error.ToMessageWithType();

                ac.WriteLog(errorLogType, errorFrom, errorText, error);
            }

            return(false);
        }
コード例 #3
0
 protected ApplicationComponent(IApplicationComponent director) : this(director.NonNull(nameof(director)).App, director)
 {
 }