一种简便的计时器和日志记录方案
来源:互联网 发布:手机淘宝排名如何上升 编辑:程序博客网 时间: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就可以实现功能了
很方便的就记录每一个方法执行的时间
求优化和更简洁的办法
- 一种简便的计时器和日志记录方案
- js读取和设置浏览器cookie的一种简便方法
- Android计时器的一种
- 找到合适的方案记录服务端日志
- 利用队列和定时器构造的一种日志记录模型实现
- 关于实现log4j2日志脱敏的一种方案
- 一种简便实效的配置文件读写方法
- 一种简便实效的配置文件读写方法
- 一种简便实效的配置文件读写方法
- Matlab一种简便的随机数生成
- struts类型转换的一种简便方法
- 一种简便获取iPhone IMEI 的方法
- [OC]Singleton的一种简便实现方式
- 一种更新yum源的简便方法
- curl的另外一种简便写法
- 记录一下互联网日志实时收集和实时计算的简单方案
- 小程序的一种轻量级记录日志的方法
- 一种用户操作日志信息的记录及读取方法
- C++实现单链表操作
- MFC概述
- 99美元Android主机Ouya在Kickstarter 8小时筹资近百万美元
- CMake添加gcov代码覆盖测试支持
- mysqldb for 27 下载地址
- 一种简便的计时器和日志记录方案
- 什么是 APN、CMWAP、CMNET?
- nginx模块开发说明
- 《代码大全》读后感4
- java中farward与redirect的区别
- PHP 扩展配置
- Unity3d之组件Component
- HNOI2002 营业额统计
- PCB设计经验