非侵入式运行时长监控-MethodTimer.Fody

NuGet installation

Install the MethodTimer.Fody NuGet package and update the Fody NuGet package:

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

Code

   [Time("DoSomethingTest:{a}")]
       private static void DoSomething(int a)
       {
       // [Time("DoSomethingTest:{a}")] 可记录参数
          Thread.Sleep(5*1000);
       }
       
       
public class MethodTimeLogger
   {
       //按时段输出
       public static void Log(MethodBase methodBase, TimeSpan elapsed, string message)
       {
           Console.WriteLine($"方法:{methodBase.Name} 耗时(时段):{elapsed}, 信息:{message}");
       }

       //按总毫秒数输出
       public static void Log(MethodBase methodBase, long milliseconds, string message)
       {
           Console.WriteLine($"方法:{methodBase.Name} 耗时(总毫秒数):{milliseconds}, 信息:{message}");
       }
   }
   
//方法:DoSomething 耗时(时段):00:00:05.0124423, 信息:DoSomethingTest

posted @ 2024-08-14 14:49  [在河之洲]  阅读(57)  评论(0)    收藏  举报