Зарегистрируйте пользовательское действие по ASP.NET приложение MVC

Ctrl + M , Ctrl + O : выйдите из строя к определениям. Я использую все это время вместе с #regions

(несмотря на , что Jeff говорит ) получить обзор кода моего экрана.

22
задан Leniel Maccaferri 28 May 2013 в 09:40
поделиться

3 ответа

Вы можете попробовать использовать аспект PostSharp , чтобы выполнить ведение журнала за вас, его функция многоадресной передачи может пригодиться для чего-то в этом роде. Если это не вариант, то, вероятно, проще всего реализовать модуль (при условии, что вы можете получить необходимую информацию о пользователе в этой точке конвейера).

6
ответ дан 16 October 2019 в 03:36
поделиться

Идея @ Рори великолепна. PostSharp - именно то, что вам нужно для этого, вы можете подумать о том, чтобы связать его с ASP.net Health Monitoring

2
ответ дан 16 October 2019 в 03:36
поделиться

Warning: This was written in 2009 for MVC .NET RC1 and may not be syntactically correct anymore.

Action Filter Attributes are perfect for this, just put a call to [YourAttributeName] at the top of your controller (or if you have an Application Controller which your other controllers inherit from, you only need it once in your application).

For example:

namespace the_name_space
{
    [Log]
    public class ApplicationController : Controller
    {

From this point onwards this attribute will be called before each action is run in your controller(s), you can also specify this on just an action by calling [Log] just before it in the same fashion.

To get the logging functionality which you require, you will most likely want to override OnResultExecuting and OnResultExecuted, both of which are pretty self explanatory.

For example:

public class LogAttribute : ActionFilterAttribute
{
    protected DateTime start_time;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        start_time = DateTime.Now;
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        RouteData route_data = filterContext.RouteData;
        TimeSpan duration = (DateTime.Now - start_time);
        string controller = (string)route_data.Values["controller"];
        string action = (string)route_data.Values["action"];
        DateTime created_at = DateTime.Now;
        //Save all your required values, including user id and whatnot here.
        //The duration variable will allow you to see expensive page loads on the controller, this can be useful when clients complain about something being slow.
    }
}
45
ответ дан 16 October 2019 в 03:36
поделиться
Другие вопросы по тегам:

Похожие вопросы: