Custom Filter In MVC Or Logging Using MVC Filter

I want to create a filter in MVC project which logs activity of each and every controller action. I am just injecting an object of type ILogger using which I am logging the logs in my Azure table storage.

This blog shows how to create action filter and how we can add it globally or in any controller or in any action.

Create an Action filter class:
  1. namespace Filter {  
  2.     using System;  
  3.     using Microsoft.AspNetCore.Mvc.Filters;  
  4.     using Microsoft.AspNetCore.Routing;  
  5.     using Core.Logging;  
  6.     /// <summary>  
  7.     /// This is tog every action activity  
  8.     /// </summary>  
  9.     /// <seealso cref="Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute" />  
  10.     [AttributeUsage(AttributeTargets.Class)]  
  11.     public class LogActionFilterAttribute: ActionFilterAttribute {  
  12.         private readonly ILogger logger;  
  13.         /// <summary>  
  14.         /// Initializes a new instance of the <see cref="LogActionFilterAttribute" /> class.  
  15.         /// </summary>  
  16.         /// <param name="logger">The logger.</param>  
  17.         public LogActionFilterAttribute(ILogger logger) {  
  18.             this.logger = logger;  
  19.         }  
  20.         /// <summary>  
  21.         /// Called when [action executing].  
  22.         /// </summary>  
  23.         /// <param name="context">The filter context.</param>  
  24.         public override void OnActionExecuting(ActionExecutingContext context) {  
  25.             this.Log("OnActionExecuting", context.RouteData);  
  26.             base.OnActionExecuting(context);  
  27.         }  
  28.         /// <summary>  
  29.         /// Called when [action executed].  
  30.         /// </summary>  
  31.         /// <param name="context"></param>  
  32.         /// <inheritdoc />  
  33.         public override void OnActionExecuted(ActionExecutedContext context) {  
  34.             this.Log("OnActionExecuted", context.RouteData);  
  35.             base.OnActionExecuted(context);  
  36.         }  
  37.         /// <summary>  
  38.         /// Called when [result executing].  
  39.         /// </summary>  
  40.         /// <param name="context">The filter context.</param>  
  41.         public override void OnResultExecuting(ResultExecutingContext context) {  
  42.             this.Log("OnResultExecuting", context.RouteData);  
  43.             base.OnResultExecuting(context);  
  44.         }  
  45.         /// <summary>  
  46.         /// Called when [result executed].  
  47.         /// </summary>  
  48.         /// <param name="context">The filter context.</param>  
  49.         public override void OnResultExecuted(ResultExecutedContext context) {  
  50.             this.Log("OnResultExecuted", context.RouteData);  
  51.             base.OnResultExecuted(context);  
  52.         }  
  53.         /// <summary>  
  54.         /// Logs the specified method name.  
  55.         /// </summary>  
  56.         /// <param name="methodName">Name of the method.</param>  
  57.         /// <param name="routeData">The route data.</param>  
  58.         private void Log(string methodName, RouteData routeData) {  
  59.             var controllerName = routeData.Values["controller"];  
  60.             var actionName = routeData.Values["action"];  
  61.             string message = $ "MethodName :{methodName} , controller:{controllerName} , action:{actionName}";  
  62.             this.logger.log(message);  
  63.         }  
  64.     }  
  65. }  
How to make any controller to use this action filter:

Method 1 

If you want the functionality to enable for all controllers by default

In starup.cs write the below line:
  1. services.AddMvc(options =>  
  2. {  
  3.    options.Filters.Add(typeof(LogActionFilterAttribute));  
  4. });  
Method 2 

If you want to use it in Controller specifically:

In starup.cs write the below line:
  1. services.AddScoped<LogActionFilterAttribute>();    
In Action Method or controller use the below attribute:
  1. [ServiceFilter(typeof(ExampleFilterWithDI))]  
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now