title: ASP.NET Core Logging With ActionFilter
body: <p>I am trying to add a logging function to save to my SQL server database. I tried to create ActionFilter  class and apply on one of my controller but not working.
  i want to capture userid, IP address, controller and action visit and timestamp. What am i missing?</p>
  
  <pre><code>AuditAttribute Action Filter class
  
  public class AuditAttribute : ActionFilterAttribute
  {
      private readonly ApplicationDbContext _db;
  
      //Inject ApplicationDBContext
      public AuditAttribute(ApplicationDbContext db)
      {
          _db = db;
      }
  
      IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
  
           //Stores the Request in an Accessible object
           var request = filterContext.HttpContext.Request;
  
          //Generate an audit
          Portal_Logger audit = new Portal_Logger()
          {
              teacherNRIC = filterContext.HttpContext.User.Identity.Name,
              IPAddress = Convert.ToString(ipHostInfo.AddressList.FirstOrDefault(address =&gt; address.AddressFamily == AddressFamily.InterNetwork)),
              ControllerAccess = (string)filterContext.RouteData.Values["controller"],
              Timestamp = DateTime.Now,
          };
  
           public DbSet&lt;Portal_Logger&gt; Portal_LoggerDBSet { get; set; }
          //Store objects to database
          _db.Portal_LoggerDBSet.Add(audit);
          _db.SaveChanges();
  
          base.OnActionExecuting(filterContext);
      }
  
  }
  }     
  
  
  Home controller class
  [AuditAttribute]
  public class HomeController : Controller
  { ..content of controller
  }
  
  
  Portal_logger model class
  public class Portal_Logger
  {
      [Key]
      public int LoggerId { get; set; }
  
      [StringLength(10)]
      public string userid{ get; set; }
  
      [StringLength(50)]
      public string IPAddress { get; set; }
  
      [StringLength(50)]
      public string ControllerAccess { get; set; }
  
      public DateTime? Timestamp { get; set; }
  
  }
  </code></pre>
  
  <p>Please see the error message as attached image
  <a href="https://i.stack.imgur.com/iETGt.png" rel="nofollow noreferrer">Screenshot of error message</a></p>
  
  <p>I tried and have a new issue.
  <a href="https://i.stack.imgur.com/94XCV.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>I tried this method also not working.
      [AuditActionFilter] -> Error message (AuditActionFilter is not an attribute class)</p>
  
  <pre><code>public class HomeController : Controller
  {
  }
  </code></pre>
  
  <hr>
  
  <p>I tried this method but not working still.</p>
  
  <pre><code>[AuditActionFilter] - error message -&gt; "AuditActionFilter" is not an 
  public class HomeController : Controller
  {
  }
  </code></pre>
  
  <p>I have added services.AddMVC to startup.cs<br>
  <code>services.AddMvc(options =&gt; options.Filters.Add(typeof(AuditActionFilter)));</code></p>
  
  <p>This is the AuditActionFilter.cs</p>
  
  <pre><code>public class AuditActionFilter : IActionFilter
  {
      private readonly ApplicationDbContext _db;
  
      public AuditActionFilter(ApplicationDbContext db)
      {
          _db = db;
      }
  
      IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
      public void OnActionExecuting(ActionExecutingContext filterContext)
      {
  
          var request = filterContext.HttpContext.Request;
  
          Portal_Logger audit = new Portal_Logger()
          {
              teacherNRIC = filterContext.HttpContext.User.Identity.Name,
              IPAddress = Convert.ToString(ipHostInfo.AddressList.FirstOrDefault(address =&gt; address.AddressFamily == AddressFamily.InterNetwork)),
              ControllerAccess = (string)filterContext.RouteData.Values["controller"],
              Timestamp = DateTime.Now,
          };
  
          //Store objects to database
          _db.Portal_LoggerDBSet.Add(audit);
          _db.SaveChanges();
      }
  
      public void OnActionExecuted(ActionExecutedContext context)
      {
          // do something after the action executes or leave it empty
      }
  
  }
  </code></pre>