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

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

01 项目简介

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

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

02 使用方法

1、添加依赖库

复制代码
PM> Install-Package Fody
PM> 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

  • End -

推荐阅读

5个基于.Net Core值得推荐的CMS开源项目

盘点5个C#开发的、可用于个人博客的系统

一个基于.Net Core+Vue+Element Ui开发的OA系统

盘点3个.Net开发的WMS仓库管理系统

5个yyds的.Net商城开源项目

相关推荐
饮水桶_T2 小时前
JAVA IO流笔记
开发语言·数据结构·算法
Felven5 小时前
C. Brr Brrr Patapim
c语言·开发语言
路溪非溪6 小时前
python标准库使用总结
开发语言·python
SomeOtherTime8 小时前
Rust 面向对象介绍(AI回答)
开发语言·python·rust
zh_xuan8 小时前
kotlin常见面试题
开发语言·kotlin·面试题
草莓熊Lotso10 小时前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
Jeff-Tsai10 小时前
Day 17:Connector 与调用端的职责收敛
.net·架构设计·erp·definition-driven
huaqianzkh11 小时前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
沐晓时光21 小时前
C语言入门,内存函数知识讲解
c语言·开发语言
David猪大卫1 天前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习