void BeginRequestOccur(object sender, EventArgs e) { HttpApplication app = (HttpApplication)sender; HttpContext context = app.Context; //处理DEBUG模式 if (base.IsDebugRequest(context)) { return; } //如果启用URL过滤器 if (useFilter) { if (urlFilter != null) { //如果不是需要处理的URL if (!urlFilter.IsAllowed(context.Request.RawUrl)) { context.Items.Add(SKIP_REQUEST, true); return; } } } //在Request的开始, 启动一个计时器 Stopwatch watch = new Stopwatch(); watch.Start(); context.Items.Add("Watch", watch); StreamWatcher watcher = new StreamWatcher(context.Response.Filter); context.Response.Filter = watcher; }
void EndRequestOccur(object sender, EventArgs e) { HttpApplication app = (HttpApplication)sender; HttpContext context = app.Context; //如果是需要跳过或处于DEBUG页面时,直接结束掉 if (context.Items.Contains(SKIP_REQUEST)) { //如果存在, 且值为True, 那么直接返回 if ((bool)context.Items[SKIP_REQUEST]) { return; } } if (context.Items.Contains(DEBUG)) { return; } long duration = 0L; //获取执行时间 if (context.Items.Contains("Watch")) { Stopwatch watch = (Stopwatch)context.Items["Watch"]; if (watch.IsRunning) { watch.Stop(); duration = watch.ElapsedMilliseconds; } } //获取请求地址 string callUrl = base.GetPureUrl(context.Request.RawUrl); long requestSize = context.Request.CountRequestSize(); long responseSize = 0; //当IIS版本为IIS7和IIS8时 if (iisVersion == WebServerType.IIS7 || iisVersion == WebServerType.IIS8) { responseSize = context.Response.GetCombineHeaders(true).Length; } StreamWatcher watcher = (StreamWatcher)context.Response.Filter; if (context.Response.Filter is StreamWatcher) { responseSize += watcher.Length; } string accesstoken = "", callAccesstoken = ""; int callAppId = 0; GetAccessTokenAndCallAccessToken(out accesstoken, out callAccesstoken, out callAppId); //推送数据 GathererContext.Current.AppendApiCall( accesstoken, callAccesstoken, callAppId, callUrl, context.Response.StatusCode, responseTime: duration, requestSize: requestSize, responseSize: responseSize); }
void BeginRequestOccur(object sender, EventArgs e) { HttpApplication app = (HttpApplication)sender; HttpContext context = app.Context; //处理DEBUG模式 if (base.IsDebugRequest(context)) return; //如果启用URL过滤器 if (useFilter) { if (urlFilter != null) { //如果不是需要处理的URL if (!urlFilter.IsAllowed(context.Request.RawUrl)) { context.Items.Add(SKIP_REQUEST, true); return; } } } //在Request的开始, 启动一个计时器 Stopwatch watch = new Stopwatch(); watch.Start(); context.Items.Add("Watch", watch); StreamWatcher watcher = new StreamWatcher(context.Response.Filter); context.Response.Filter = watcher; }