如何在MVC中跟踪方法调用?

时间:2017-05-21 09:25:59

标签: asp.net-mvc

如何跟踪模型类中编写的所有操作方法和方法在项目执行期间获得的内容以及该特定方法的执行时间(模型类中调用的操作方法/方法)

1 个答案:

答案 0 :(得分:2)

简单回答:自定义过滤器

以下是我实施的用于记录/跟踪所有活动的自定义过滤器的一个示例 - 比如哪些操作方法正在执行,以及它们何时被执行。

基本上,我创建了一个自定义过滤器属性 - “TrackExecutionTime”。我已经为Action,Result甚至Exception过滤器实现了这个功能。我希望你正在寻找这样的东西 -

自定义过滤器代码 - TrackExecutionTime.cs

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);
    }
}

我使用此自定义过滤器属性的控制器代码 -

public class HomeController : Controller
{
    [TrackExecutionTime]
    public ActionResult Index()
    {
        return View();
    }

    [TrackExecutionTime]
    public string Welcome()
    {
        throw new Exception("new exception is occured.");
    }
}

执行Action方法后记录的数据 -

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

希望这有帮助。如果它可以帮助您解决问题,那么不要忘记将其标记为答案。