Lia*_*amB 15 asp.net asp.net-mvc
我正在寻找一种方法来跟踪服务器生成页面所需的时间.我知道我可以使用Trace跟踪这个,但我需要一种方法来显示每页.
其ASP.Net MVC 2
Ken*_*son 15
您可以像ActionFilterAttribute一样实现它
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class LoggingAttribute : ActionFilterAttribute
{
private readonly Stopwatch _sw;
public LoggingAttribute()
{
_sw = new Stopwatch();
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
_sw.Start();
Debug.WriteLine("Beginning executing: " + GetControllerAndActionName(filterContext.ActionDescriptor));
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
_sw.Stop();
var ms = _sw.ElapsedMilliseconds;
Debug.WriteLine("Finishing executing: " + GetControllerAndActionName(filterContext.ActionDescriptor));
Debug.WriteLine("Time elapsed: "+ TimeSpan.FromMilliseconds(ms).TotalSeconds);
}
private string GetControllerAndActionName(ActionDescriptor actionDescriptor)
{
return actionDescriptor.ControllerDescriptor.ControllerName + " - " + actionDescriptor.ActionName;
}
}
Run Code Online (Sandbox Code Playgroud)
用它来装饰每个控制器或动作方法,瞧,它在调试中吐出文本.
编辑:如果要在页面上打印它,您可以将此片段添加到OnActionExecuted方法
if(filterContext.Result is ViewResult) { //Make sure the request is a ViewResult, ie. a page
((ViewResult) filterContext.Result).ViewData["ExecutionTime"] = ms; //Set the viewdata dictionary
}
Run Code Online (Sandbox Code Playgroud)
现在你已经在ViewData中保存了执行时间,并且可以在页面中访问它.我通常把它放在这样的母版页中
<!-- The page took <%= ViewData["ExecutionTime"] %> ms to execute -->
Run Code Online (Sandbox Code Playgroud)
JOB*_*OBG 14
是的,Derin Suggestion是在ASP.NEt应用程序中执行此操作的标准方法,我建议添加一个if if它不会干扰非HTML响应:编辑:添加完整的实现
public class PerformanceMonitorModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += delegate(object sender, EventArgs e)
{
//Set Page Timer Star
HttpContext requestContext = ((HttpApplication)sender).Context;
Stopwatch timer = new Stopwatch();
requestContext.Items["Timer"] = timer;
timer.Start();
};
context.PostRequestHandlerExecute += delegate(object sender, EventArgs e)
{
HttpContext httpContext = ((HttpApplication)sender).Context;
HttpResponse response = httpContext.Response;
Stopwatch timer = (Stopwatch)httpContext.Items["Timer"];
timer.Stop();
// Don't interfere with non-HTML responses
if (response.ContentType == "text/html")
{
double seconds = (double)timer.ElapsedTicks / Stopwatch.Frequency;
string result_time = string.Format("{0:F4} sec ", seconds);
RenderQueriesToResponse(response,result_time);
}
};
}
void RenderQueriesToResponse(HttpResponse response, string result_time)
{
response.Write("<div style=\"margin: 5px; background-color: #FFFF00\"");
response.Write(string.Format("<b>Page Generated in "+ result_time));
response.Write("</div>");
}
public void Dispose() { /* Not needed */ }
}
Run Code Online (Sandbox Code Playgroud)
你也可以添加一些风格......
并记得在httpModules部分的WebConfig中注册您的模块:
<add name="Name" type="namespace, dll"/>
Run Code Online (Sandbox Code Playgroud)
有关此内容的完整参考,请参阅Steven Sanderson的Pro ASP.NET MVC框架 - 第15章 - 性能,监控页面生成时间.
编辑:(评论@Pino)以下是我的项目示例: alt text http://www.diarioplus.com/files/pictures/example_performance.JPG
归档时间: |
|
查看次数: |
6661 次 |
最近记录: |