Asp.net mvc 如何在MVC中跟踪方法调用?
如何跟踪项目执行期间所有动作方法和模型类中编写的方法得到了什么,以及该特定方法(动作方法/模型类中调用的方法)的执行时间是多少 下面是一个自定义过滤器的示例,我已经实现了它来记录/跟踪所有活动,比如执行哪些操作方法,在什么时候执行它们 基本上,我已经创建了一个定制的过滤器属性——“TrackExecutionTime”。我已经为操作、结果甚至异常过滤器实现了这个功能。我希望你在找这样的东西-- 自定义筛选器代码--TrackExecutionTime.csAsp.net mvc 如何在MVC中跟踪方法调用?,asp.net-mvc,Asp.net Mvc,如何跟踪项目执行期间所有动作方法和模型类中编写的方法得到了什么,以及该特定方法(动作方法/模型类中调用的方法)的执行时间是多少 下面是一个自定义过滤器的示例,我已经实现了它来记录/跟踪所有活动,比如执行哪些操作方法,在什么时候执行它们 基本上,我已经创建了一个定制的过滤器属性——“TrackExecutionTime”。我已经为操作、结果甚至异常过滤器实现了这个功能。我希望你在找这样的东西-- 自定义筛选器代码--TrackExecutionTime.cs public class TrackE
public class TrackExecutionTime : ActionFilterAttribute, IExceptionFilter
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string message = "\n" + filterContext.ActionDescriptor.ControllerDescriptor.ControllerName +
" --> " + filterContext.ActionDescriptor.ActionName + " --> OnActionExecuting \t - " +
DateTime.Now.ToString() + "\n";
LogExecutionTime(message);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
string message = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName +
" --> " + filterContext.ActionDescriptor.ActionName + " --> OnActionExecuted \t - " +
DateTime.Now.ToString() + "\n";
LogExecutionTime(message);
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
string message = "\n" + filterContext.RouteData.Values["controller"].ToString() +
" --> " + filterContext.RouteData.Values["action"].ToString() + " --> OnResultExecuting \t - " +
DateTime.Now.ToString() + "\n";
LogExecutionTime(message);
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
string message = "\n" + filterContext.RouteData.Values["controller"].ToString() +
" --> " + filterContext.RouteData.Values["action"].ToString() + " --> OnResultExecuted \t - " +
DateTime.Now.ToString() + "\n";
LogExecutionTime(message);
LogExecutionTime("--------------------------------------------");
}
public void OnException(ExceptionContext filterContext)
{
string message = "\n" + filterContext.RouteData.Values["controller"].ToString() +
" --> " + filterContext.RouteData.Values["action"].ToString() + " --> " + filterContext.Exception.Message + " \t - " +
DateTime.Now.ToString() + "\n";
LogExecutionTime(message);
LogExecutionTime("--------------------------------------------");
}
private void LogExecutionTime(string data)
{
File.AppendAllText(HttpContext.Current.Server.MapPath("~/Data/Data.txt"), data);
}
}
我使用此自定义筛选器属性的控制器代码--
执行操作方法后记录的数据--
我成功地实现了这一点,这要归功于以下资源:
http://www.pragimtech.com/mvc-video-tutorial-for-beginners.aspx
希望这是有帮助的。如果它能帮助您解决问题,请不要忘记将其标记为答案。谢谢您的回答。这对我很有帮助。但是使用它,我们只能记录操作o的信息。控制器。但是我们不能记录模型的信息。调用哪个模型、执行时间等。。有什么解决办法吗?
Home --> Index --> OnActionExecuting - 4/8/2017 4:21:57 PM
Home --> Index --> OnActionExecuted - 4/8/2017 4:21:58 PM
Home --> Index --> OnResultExecuting - 4/8/2017 4:21:58 PM
Home --> Index --> OnResultExecuted - 4/8/2017 4:21:58 PM
--------------------------------------------
Home --> Welcome --> OnActionExecuting - 4/8/2017 4:22:21 PM
Home --> Welcome --> OnActionExecuted - 4/8/2017 4:22:21 PM
Home --> Welcome --> new exception is occured. - 4/8/2017 4:22:21 PM
--------------------------------------------
http://www.pragimtech.com/mvc-video-tutorial-for-beginners.aspx