首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

一个监控C#方法运行耗时开源库

在项目开发中,我们经常需要监控某些方法的运行耗时,找出性能问题的所在地方。今天给大家,推荐一个用来统计C#方法运行耗时的开源库。

01

项目简介

一个轻量级.Net库,用于对.NET应用程序中的方法执行时间进行测量和分析,可以轻松集成到现有的.NET应用程序中。

提供了高精度的计时功能,可以准确地测量方法的执行时间,同时运行自定义标签,便于更好的识别方法。

02

使用方法

1、添加依赖库

PM> Install-Package FodyPM> Install-Package MethodTimer.Fody

2、耗时拦截器记录方法

支持两种拦截器方式:

2.1 格式:TimeSpan

public static class MethodTimeLogger1{ public static void Log(MethodBase methodBase, TimeSpan elapsed, string message) { Console.WriteLine($"方法:{methodBase.Name} 耗时:{elapsed}, 信息:{message}"); }}

2.2 格式:long(毫秒)

public static class MethodTimeLogger1{ public static void Log(MethodBase methodBase, long milliseconds, string message) { Console.WriteLine($"方法:{methodBase.Name} 耗时:{milliseconds}秒, 信息:{message}"); }}

3、使用示例

完整示例代码如下:

using MethodTimer;using System.Reflection;

//执行var testClass = new TestClass();testClass.MonitorMethod();

//监控方法public class TestClass{ [Time("测试日志")]public void MonitorMethod() { Console.WriteLine("执行完毕!"); }}

//拦截器public static class MethodTimeLogger{ public static void Log(MethodBase methodBase, TimeSpan elapsed, string message) { Console.WriteLine($"方法:{methodBase.Name} 耗时:{elapsed}, 信息:{message}"); }}

运行效果:

03

项目地址

https://github.com/Fody/MethodTimer

  • 发表于:
  • 原文链接https://page.om.qq.com/page/OSSYWEhdh1or84DR9N8TED3w0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券
http://www.vxiaotou.com