TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Margaal
NA
139
87.8k
Best ways to track user activities in my ASP.NET Core app
Jul 11 2019 5:07 AM
Currently, to track user activities, I create custom
filter attribute which based on ResultFilterAttribute. It's worked for me, but I want to know if there is no better way to perform this? Because, the documentation of .NET Core filter recommends "avoid creating and using filters purely for logging purposes."
Please give me your opinion.
My Custom Attribute
public
class
ActivitiesAttribute : ResultFilterAttribute
{
private
string
_description;
public
ActivitiesAttribute(
string
description)
{
_description = description;
}
public
override
Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
Debug.WriteLine($
"===User : {context.HttpContext.User.Identity.Name}, description : {_description}, Date {DateTime.Now.ToString()}"
);
return
base
.OnResultExecutionAsync(context, next);
}
}
My Controller
[Authorize]
public
class
ProductsController : Controller
{
private
readonly
ApplicationDbContext _context;
public
ProductsController(ApplicationDbContext context)
{
_context = context;
}
// GET: Products
[Activities(
"Products List"
)]
public
async Task<IActionResult> Index()
{
return
View(await _context.Products.ToListAsync());
}
// GET: Products/Details/5
[Activities(
"Product Detail"
)]
public
async Task<IActionResult> Details(
int
? id)
{
if
(id ==
null
)
{
return
NotFound();
}
var product = await _context.Products
.FirstOrDefaultAsync(m => m.Id == id);
if
(product ==
null
)
{
return
NotFound();
}
return
View(product);
}
// GET: Products/Create
[Activities(
"Product Creation"
)]
public
IActionResult Create()
{
return
View();
}
....
}
Reply
Answers (
5
)
allignment in rdlc report?
How to do Migration in Entity Framework core code first