面向云技术架构 - 痴者工良

  • 首页
  • 工良写的电子书
    • Istio 入门教程
    • kubernetes 教程
    • 从 C# 入门 Kafka
    • 多线程和异步
    • 动态编程-反射、特性、AOP
    • 表达式树
  • 本站文章导航
  • 隐私政策
愿有人陪你颠沛流离
遇到能让你付出的事物或者人,都是一种运气。
能遇到,就该珍惜。或许你们最终没能在一起,但你会切实地感受到力量。
正因为这样,那段相遇才变得有价值,才没有辜负这世间的每一段相遇。
  1. 首页
  2. .NET
  3. 正文

监控任务执行时长(Task)并审计

2020年10月10日 1971点热度 1人点赞 0条评论
内容纲要

编写一个拓展:

    public static class TaskExtensions
    {
        public static Task Start(this Task task, out Stopwatch stopwatch)
        {
            stopwatch = new Stopwatch();
            stopwatch.Start();
            return task;
        }

        public static Task<T> Start<T>(this Task<T> task, out Stopwatch stopwatch)
        {
            stopwatch = new Stopwatch();
            stopwatch.Start();
            return task;
        }

        public static Task<T> End<T>(this Task<T> task, MethodBase method, int time, Stopwatch stopwatch)
        {
            return End(task, method, time, stopwatch);
        }

        public static Task End(this Task task, MethodBase method, int time, Stopwatch stopwatch)
        {
            return End(task, method, time, stopwatch);
        }

        private static T End<T>(T task, MethodBase method, int time, Stopwatch stopwatch) where T : Task
        {
            task.ContinueWith(x =>
            {
                stopwatch.Stop();
                var thisTime = stopwatch.ElapsedMilliseconds;
                if (thisTime > time)
                {
                    MyTask(method.DeclaringType.DeclaringType, (MethodInfo)method, thisTime);
                }

            });
            return task;
        }

        /// <summary>
        /// 加上审计记录,执行此任务的函数位置等
        /// </summary>
        /// <param name="type"></param>
        /// <param name="info"></param>
        /// <param name="time"></param>
        private static void MyTask(Type type, MethodInfo info, long time)
        {
            Console.WriteLine($"执行位置 {type.Name}-{info.DeclaringType.FullName},耗时 {time} ms");
        }
}

使用方法:

            static async Task Main(string[] args)
            {
                var method = MethodBase.GetCurrentMethod();
                for (int i = 0; i < 10; i++)
                {
                    var task = await T()
                        .Start(out var watch)
                        .End(method, 1000, watch);
                    Console.WriteLine($"第 {i} 次执行");
                }
            }

            /// <summary>
            /// 被执行的任务
            /// </summary>
            /// <returns></returns>
            private static async Task<string> T()
            {
                await Task.Delay(new Random().Next(980, 1001));
                return "aaaa";
            }

这样可以记录每个任务执行的时间,并且记录执行位置,方便对性能进行分析和定位异常位置。

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: task 任务 审计 执行 监控
最后更新:2021年2月21日

痴者工良

高级程序员劝退师

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2023 whuanle.cn. ALL RIGHTS RESERVED.

粤ICP备18051778号

粤公网安备 44030902003257号