ASP.NET MVC 3–Global Action Filters

本文介绍了一个用于ASP.NET MVC应用程序的性能剖析工具实现。该工具通过全局过滤器捕获请求数据,并记录详细的请求信息,如URL、请求数据、响应状态码等。使用ProfilerContext上下文管理器和ProfileData类来收集和存储数据,最后将数据持久化到内存中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class MvcProfilerGlobalAttribute : ActionFilterAttribute
{
//...
}

protected void Application_Start()
{
    //...
    GlobalFilters.Filters.Add(new MvcProfilerGlobalAttribute());
}


public class MvcProfilerGlobalAttribute : ActionFilterAttribute
  {
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          var httpContext = filterContext.RequestContext.HttpContext;
          var httpRequest = httpContext.Request;
          var inputStream = httpRequest.InputStream;
          var requestBody = new StreamReader(inputStream).ReadToEnd();
          inputStream.Seek(0, SeekOrigin.Begin);
          }


public class ProfileData
   {
       public string Url { get; set; }
       public string RequestData { get; set; }
       public string ResponseHttpStatusCode { get; set; }
       public string RequestHeaders { get; set; }
       public string ResponseHeaders { get; set; }
       public string QueryStringData { get; set; }
       public string ClientIp { get; set; }
       public DateTime StartTime { get; set; }
       public DateTime EndTime { get; set; }
   }
 public class ProfilerContext : IDisposable
   {
       private readonly IProfilerLogRepository repository;
       private const string ProfilerItemKey = "DEVELOQPROFILER";
       private readonly Guid profilerContextIdentifier;
       private readonly DateTime started;

       public ProfileData ProfileData { get; private set; }

       public ProfilerContext() : this(new InMemoryProfilerLogRepository()) { }

       public ProfilerContext(IProfilerLogRepository repository)
       {
           this.repository = repository;
           profilerContextIdentifier = Guid.NewGuid();
           started = DateTime.UtcNow;
           ProfileData = new ProfileData();
       }

       public void Persist()
       {
           repository.Add(ProfileData);
       }

       public void Dispose()
       {
           Persist();
       }

       public static ProfilerContext Current
       {
           get
           {
               var context = HttpContext.Current;
               if (context == null) return null;
               InitCurrentProfiler();
               return context.Items[ProfilerItemKey] as ProfilerContext;
           }
           private set
           {
               var context = HttpContext.Current;
               if (context == null) return;

               context.Items[ProfilerItemKey] = value;
           }
       }

       private static void InitCurrentProfiler()
       {
           var context = HttpContext.Current;
           if (context == null) return;
           if (context.Items[ProfilerItemKey] as ProfilerContext != null) return;

           Current = new ProfilerContext();
       }


   }

public interface IProfilerLogRepository
    {
        void Add(ProfileData profileData);
        List<profiledata> GetAll();
    }
public class InMemoryProfilerLogRepository : IProfilerLogRepository
    {
        private static List<profiledata> profileLogs;

        public InMemoryProfilerLogRepository()
        {
            if (profileLogs == null)
                profileLogs = new List<profiledata>();
        }

        public void Add(ProfileData profileData)
        {
            profileLogs.Add(profileData);
        }

        public List<profiledata> GetAll()
        {
            return profileLogs;
        }
    }

 

转载于:https://www.cnblogs.com/yezhi/archive/2013/01/23/2874053.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值