一种简便的计时器和日志记录方案

在做效能分析的时候

我们经常需要统计每个方法的执行效率

通常的情况我们都是这样的:

            StopWatcher sw = new StopWatcher();
            sw.start();
            try
            {
                //dosth
            }
            finally
            {
                sw.stop();
                Logger.LogProcess("xxx", sw.getTime());
            }

每一个方法都需要最外层添加一层

加上try-finally

很麻烦

有没有一种更简单的实现方法呢?

苦思冥想了一段时间

最近终于找到了一种相对来说稍微要简易一点的办法...


1.封装stopwatch

    public class StopWatcher
    {
        private Stopwatch sw ;

        public StopWatcher()
        {
            sw = new Stopwatch();
        }

        public void start()
        {
            if (sw.IsRunning)
            {
                sw.Stop();
                sw.Reset();
            }
            sw.Reset();
            sw.Start();
        }

        public void stop()
        {
            sw.Stop();
        }
        
        public int getTime()
        {
            return (int)sw.ElapsedMilliseconds;
        }
    }

2.再次进行封装:

public class StopWatcherAuto:IDisposable
    {
        private StopWatcher sw = null;


        public StopWatcherAuto()
        {
            try
            {
                sw = new StopWatcher();
                sw.start();
            }
            catch
            {
            }
        }






        #region IDisposable 成员


        public void Dispose()
        {
            try
            {
                sw.stop();
                EdmLogger.LogProcess(string.Format("执行方法:{0}", GetMethod()), sw.getTime());
            }
            catch
            {
            }
        }


        #endregion


        private string GetMethod()
        {
            var method = new StackFrame(2).GetMethod();
            string result = method.Name;
            if (string.IsNullOrEmpty(result))
            {
                return string.Empty;
            }
            else
            {
                return result;
            }
        }
    }

3.封装log4net

public static class Logger
    {
        static Logger()
        {
            string path = "";
            String AssemblyPath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
            String AssemblyDir = System.IO.Path.GetDirectoryName(AssemblyPath);
            AssemblyDir = AssemblyDir.Replace("file:\\", "");
            path = AssemblyDir + "\\";
            //String AppDir = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            //if ((AssemblyDir + "\\") != AppDir)
            //{
            //    path = AssemblyDir + "\\";
            //}

            FileInfo configFile = new FileInfo(path + "log4net.config");
            log4net.Config.DOMConfigurator.ConfigureAndWatch(configFile);
            ErrorLogger = LogManager.GetLogger("ErrorLogger");
            InfoLogger = LogManager.GetLogger("InfoLogger");
            ProcessLogger = LogManager.GetLogger("ProcessLogger");
        }

        private static ILog ErrorLogger;
        private static ILog InfoLogger;
        private static ILog ProcessLogger;

        public static void LogError(string message)
        {
            ErrorLogger.Error(message);
        }

        public static void LogError(string message, Exception ex)
        {
            ErrorLogger.Error(message, ex);
        }

        public static void LogInfo(string message)
        {
            InfoLogger.Info(message);
        }

        public static void LogInfo(string message, Exception ex)
        {
            InfoLogger.Info(message, ex);
        }

        public static void LogProcess(string message, int timeSpend)
        {
            ProcessLogger.Info(string.Format("完成 {0} 操作,共计耗时:{1}",message,timeSpend.ToString()));
        }

        public static void LogProcess(string message)
        {
            ProcessLogger.Info(message);
        }

4.使用

using (StopWatcherAuto sw = new StopWatcherAuto())
            {
                Logger.LogProcess("dosth");
            }

这样的好处就是只需要在最外层添加一个using就可以实现功能了

很方便的就记录每一个方法执行的时间


求优化和更简洁的办法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值