前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ASP.NET Web API 接口执行时间监控

ASP.NET Web API 接口执行时间监控

作者头像
张善友
发布2018-01-22 15:45:26
2K0
发布2018-01-22 15:45:26
举报
文章被收录于专栏:张善友的专栏张善友的专栏

软件产品常常会出现这样的情况:产品性能因某些无法预料的瓶颈而受到干扰,导致程序的处理效率降低,性能得不到充分的发挥。如何快速有效地找到软件产品的性能瓶颈,则是我们感兴趣的内容之一。

在本文中,我将解释我如何清理和替换重复、 混乱遍布许多方法在应用程序中的代码使用ASP.NET Web API 的筛选器来完成ASP.NET Web API 接口执行时间监控。我们的项目中有如下的需求:我的工作相关的项目 (使用 ASP.NET Web API 框架) 要求记录下服务接口的调用执行时间以及请求延迟、 故障率每秒的请求总数,以帮助运营团队。 

Web API 筛选器是你放到Action上的自定义属性方法 (或Controller) 添加常用功能。 Web API 筛选使您得以添加预处理和后处理的行为,本文的代码来自于How to intercept all the ASP.NET WebApi controller action methods calls with Ninject interception for logging?Log duration of an ASP Web API action

下面是详细代码:

using System; 
using System.Collections.Generic;    
using System.Diagnostics;    
using System.Linq;    
using System.Web;    
using System.Web.Http.Controllers;    
using System.Web.Http.Filters;
namespace ContactManager.Filters   
{    
    public class TimingActionFilter : ActionFilterAttribute    
    {    
        private const string Key = "__action_duration__";
        public override void OnActionExecuting(HttpActionContext actionContext)   
        {    
            if (SkipLogging(actionContext))    
            {    
                return;    
            }    
            var stopWatch = new Stopwatch();    
            actionContext.Request.Properties[Key] = stopWatch;    
            stopWatch.Start();    
        }
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)   
        {    
            if (!actionExecutedContext.Request.Properties.ContainsKey(Key))    
            {    
                return;    
            }
            var stopWatch = actionExecutedContext.Request.Properties[Key] as Stopwatch;   
            if (stopWatch != null)    
            {    
                stopWatch.Stop();    
                var actionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName;    
                var controllerName = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName;    
                Debug.Print(string.Format("[Execution of{0}- {1} took {2}.]", controllerName, actionName, stopWatch.Elapsed));    
            }
        }
        private static bool SkipLogging(HttpActionContext actionContext)   
        {    
            return actionContext.ActionDescriptor.GetCustomAttributes<NoLogAttribute>().Any() ||    
                    actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<NoLogAttribute>().Any();    
        }
    }
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true)]   
    public class NoLogAttribute : Attribute    
    {
    }   
}

然后在代码里注册 GlobalConfiguration.Configuration.Filters.Add(new TimingActionFilter());

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-09-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档