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

来源:互联网 发布:手机淘宝排名如何上升 编辑:程序博客网 时间:2024/06/05 21:57

在做效能分析的时候

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

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

            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就可以实现功能了

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


求优化和更简洁的办法

原创粉丝点击