Asp.Net MVC How to log all actions being called

asp.net web api log all requests
asp net mvc log raw request
web api logging best practices
c# log http request body
asp net core log request body
actionfilterattribute
get action parameters in action filter
httpcontext current request log

I need to be able to log all actions that are called from my asp.net mvc application. How and what would be the best way to achieve this? Where I log it to whether it be the console or log file doesn't matter.

You could create your own class which inherits from ActionFilterAttribute and then override the OnActionExecuting method.

Example

public class LogActionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

        var controller = filterContext.RequestContext.RouteData.Values["Controller"];
        var action = filterContext.RequestContext.RouteData.Values["Action"];

        //
        // Perform logging here
        //

        base.OnActionExecuting(filterContext);
    }
}

public class HomeController : Controller
{
    [LogAction]
    public ActionResult Index()
    {

        return View();
    }

}

Hope this helps!

Log all data in ASP.Net MVC POST Requests, It is just another Action Filter that can be applied globally, per controller or per action: using System.Linq; using System.Web.Mvc; using� Exercise 1: Logging Actions. In this exercise, you will learn how to create a custom action log filter by using ASP.NET MVC 4 Filter Providers. For that purpose you will apply a logging filter to the MusicStore site that will record all the activities in the selected controllers.

You could try Audit.NET library with its Audit.MVC and the different data providers to store the logs on files, eventlog, sql, redis, mongo, and much more.

With the MVC extension you just need to decorate your controllers or actions with an attribute:

[Audit]
public class HomeController : Controller
{ ... }

Execute a static configuration to set the output of your logs:

Audit.Core.Configuration.Setup()
    .UseFileLogProvider(_ => _
        .Directory(@"C:\Logs"));

And it will provide the infrastructure to log the interactions with your MVC application.

How to log your user's controller actions for an ASP.Net MVC 4 , Net MVC 4 Here's the code I'm using to create a log of user activity. Now that the attribute exists, we need to have it called before every action. showing you a log of the actions your users run when they use your ASP. from the actions, the wizard knows what to do (this is possible if you implement a wizard pattern). Navigate() being a method defined on a base WizardController class. The reason this works is that, in essence, step info gets serialized to the page with each request (AJAX or not), and is deserialized when the controller reads the response in

Credit HeyMega for their answer. Here's an example of an expanded implementation I arrived at in MVC5.

public class LogActionAttribute : ActionFilterAttribute
{

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var controller = filterContext.RequestContext.RouteData.Values.ContainsKey("Controller") ? filterContext.RequestContext.RouteData.Values["Controller"].ToString() : null;
        var action = filterContext.RequestContext.RouteData.Values.ContainsKey("Action") ? filterContext.RequestContext.RouteData.Values["Action"].ToString() : null;
        var area = filterContext.RequestContext.RouteData.DataTokens.ContainsKey("Area") ? filterContext.RequestContext.RouteData.DataTokens["Area"].ToString() : null;
        var user = filterContext.RequestContext.HttpContext.User.Identity.GetUserId();

        Task.Run(() => Generic().AreaActionLog(user, area, controller, action));

        base.OnActionExecuting(filterContext);
    }
}

I chose to separate the method doing the actual logging into a separate process, if anything goes wrong with the Database interaction, or the DB interaction takes several seconds, the UI is uninterrupted.

You can then decorate the entire controller with [LogAction] attribute like so.

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

    public ActionResult Contact()
    {
        return View();
    }
}

Or selectively apply the attribute by decorating individual methods:

public class HomeController : Controller
{
    [LogAction]
    public ActionResult Index_Logs_Things()
    {
        return View();
    }
}

Hope this helps someone.

How to log request and response metadata in ASP.NET Web API , The message handler will be used to intercept calls and log all requests and responses centrally in one place. Another way is to use a custom action filter. [ Also on InfoWorld: Git tutorial: Get started with Git version control ] NET Core MVC � How to implement global exception handling in ASP. Our company has the need to log certain things each time one of our action methods of our ASP.NET WebApi controllers gets called. Since we use Ninject for the DI right now, we'd like to use it also for this purpose. This is what I have tried so far.

Logging Application Utilization of an ASP.NET MVC 5 Web Application, The Log4Net library can been added to any ASP. In ASP.NET MVC, controllers define action methods that usually have a request is routed to the designated controller, and the corresponding action method is called. and how the application can be set up to log all user actions through an action filter. ASP.NET MVC creates objects using the model binding process with the data which is sent by the browser in an HTTP request. The action method parameters are created through model binding from the data in the request. Setting up the project. I created a new ASP.NET MVC project with the empty template and add folders and core references for MVC.

Custom Filter In MVC Or Logging Using MVC Filter, logger = logger;; }; /// <summary>; /// Called when [action executing]. In ASP.NET MVC, controllers define action methods that usually have a one-to-one relationship with possible user interactions, such as clicking a link, or submitting a form. For example, when a user clicks a link, a request is routed to the designated controller, and the corresponding action method is called.

Global Action Filters in Asp.Net MVC for Exception Handling and , Global Action Filters in Asp.Net MVC for Exception Handling and Logging In asp.net mvc we can apply global action filters to all action methods in our class we are going to create an Exception handler filter which will get called if any error � However, in the loose sense, the word action filter is used to refer to any type of filter in the ASP.NET MVC framework. The base ActionFilterAttribute class has the following methods that you can override: OnActionExecuting – This method is called before a controller action is executed.

Comments
  • Nice THOROUGH answer ;)
  • To anyone wanting to be extremely loggy (like myself), you can add the [LogAction] attribute to above the entire class like so [LogAction} public class HomeController : Controller{} rather than individual controller.