ASP.NET MVC WEBAPI执行时间过滤器

为了方便给全部接口加入执行时间的监控,加一个过滤器就好了。

using System;
using System.Diagnostics;
using System.Linq;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace api.Filters
{
    /// <summary>
    /// 页面执行时间
    /// </summary>
    public class TimingActionFillter : 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;
                //var parameters = actionExecutedContext.ActionContext.Request.RequestUri.Host;
                //var response = actionExecutedContext.ActionContext.Response.Content.ToString();
                //Debug.Print(string.Format("[Execution of{0}- {1} took {2}.]", controllerName, actionName, stopWatch.Elapsed));

                string content = $" {AppendSpace(controllerName, 30)}| {AppendSpace(actionName,30)}| { AppendSpace(stopWatch.ElapsedMilliseconds+"ms",12)}| ";
                content += $"level={AppendSpace((stopWatch.ElapsedMilliseconds / 100).ToString(), 12)}| ";
                //content += $"request={parameters}| ";
                //content += $"response={response}";
                PT.Utilities.LogHelper.TimeLog(content);
            }

        }

        private static bool SkipLogging(HttpActionContext actionContext)
        {
            return actionContext.ActionDescriptor.GetCustomAttributes<NoLogAttribute>().Any() ||
                    actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<NoLogAttribute>().Any();
        }

        private string AppendSpace(string str, int length)
        {
            int strLen = str.Length;
            str = (str ?? "").Trim();
            if (str.Length >= length) return str;
            for (int i = 0; i < length - strLen; i++)
                str += " ";
            return str;
        }
    }

    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true)]
    public class NoLogAttribute : Attribute
    {

    }
}

然后在对应的类名上面写上[TimingActionFillter] ,只有继承apicontroller才会生效,注意替换日志写入类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值