Пример #1
0
        /// <summary>
        /// 结束计时并打印日志
        /// </summary>
        /// <param name="counterToken"></param>
        /// <param name="msg"></param>
        /// <param name="level"></param>
        public static void StopCounterAndLog(CounterToken counterToken, string msg, LogLevel level = LogLevel.Info)
        {
            var elapsed = StopCounter(counterToken);

            if (!msg.Contains("{0}"))
            {
                msg += " {0}";
            }
            LogAgent.Write(level, msg, elapsed.TotalMilliseconds);
        }
Пример #2
0
        /// <summary>
        /// 结束计时
        /// </summary>
        /// <param name="counterToken"></param>
        /// <returns></returns>
        public static TimeSpan StopCounter(CounterToken counterToken)
        {
            if (!counters.ContainsKey(counterToken))
            {
                return(TimeSpan.Zero);
            }

            var sw = counters[counterToken];

            sw.Stop();

            counters.Remove(counterToken);

            return(sw.Elapsed);
        }
Пример #3
0
        /// <summary>
        /// 开始计时
        /// </summary>
        /// <returns></returns>
        public static CounterToken StartCounter()
        {
            var t = new CounterToken
            {
                Id = Guid.NewGuid().ToString()
            };

            var sw = new Stopwatch();

            counters.Add(t, sw);

            sw.Start();

            return(t);
        }