一个监控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商城开源项目

相关推荐
Ray Liang9 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
赵榕11 小时前
ClaimsPrincipal序列化为Json的正确姿势
.net
追逐时光者1 天前
一款使用 C# 编写专为 Windows 11 打造的文件资源管理器增强工具!
后端·.net
Scout-leaf3 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530143 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
牧马人win4 天前
SmartDapper.Repository
.net
mudtools5 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的5 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书