示例#1
0
        public static Exception RunOperation(string identity, string label, Action action, IDxStoreEventLogger logger, LogOptions options, bool isBestEffort = false, TimeSpan?timeout = null, TimeSpan?periodicDuration = null, string periodicKey = null, Action <Exception> exitAction = null, string context = null)
        {
            bool flag       = false;
            bool flag2      = false;
            bool flag3      = false;
            bool isPeriodic = false;

            if ((options & LogOptions.LogNever) != LogOptions.LogNever)
            {
                flag  = ((options & LogOptions.LogStart) == LogOptions.LogStart);
                flag2 = ((options & LogOptions.LogException) == LogOptions.LogException);
                flag3 = ((options & LogOptions.LogSuccess) == LogOptions.LogSuccess);
                if ((options & LogOptions.LogAlways) == LogOptions.LogAlways)
                {
                    isPeriodic = ((options & LogOptions.LogPeriodic) == LogOptions.LogPeriodic);
                }
            }
            periodicKey = (periodicKey ?? string.Format("{0}-{1}", identity, label));
            Exception ex = null;

            try
            {
                ExTraceGlobals.RunOperationTracer.TraceDebug <string, string>((long)identity.GetHashCode(), "{0}: Attempting to run {1}", identity, label);
                if (flag)
                {
                    Utils.LogStartingEvent(identity, label, logger, isPeriodic, periodicKey, periodicDuration, context);
                }
                if (timeout != null)
                {
                    Task task = Task.Factory.StartNew(action);
                    if (!task.Wait(timeout.Value))
                    {
                        ExTraceGlobals.RunOperationTracer.TraceError <string, string, TimeSpan?>((long)identity.GetHashCode(), "{0}: Operation {1} timed out after {2}", identity, label, timeout);
                        throw new TimeoutException();
                    }
                }
                else
                {
                    action();
                }
            }
            catch (Exception ex2)
            {
                ex = ex2;
                ExTraceGlobals.RunOperationTracer.TraceError <string, string, Exception>((long)identity.GetHashCode(), "{0}: Operation {1} failed with {2}", identity, label, ex2);
                if (flag2)
                {
                    Utils.LogErrorEvent(identity, label, ex, options, logger, isPeriodic, periodicKey, periodicDuration, context);
                }
                if (!isBestEffort)
                {
                    throw;
                }
            }
            finally
            {
                if (ex == null)
                {
                    ExTraceGlobals.RunOperationTracer.TraceDebug <string, string>((long)identity.GetHashCode(), "{0}: Operation {1} successful", identity, label);
                    if (flag3)
                    {
                        Utils.LogFinishedEvent(identity, label, logger, isPeriodic, periodicKey, periodicDuration, context);
                    }
                }
                if (exitAction != null)
                {
                    exitAction(ex);
                }
            }
            return(ex);
        }