I want to create a log system not to log exceptions , but user activities and what they do upon data . for example when a user deletes a record , I want to log username , records id , date and … .
first I decided to create a log class and use its methods inside all other classes methods . but after some reading blogs and searching SO , I figured out that this approach is against SRP principle of OOP .
now I’m thinking of another solution . I still have my log class . but instead of calling its methods all over the codes , I should use events . I’m thinking of adding a listener class that subscribes to events that are invoked by other classes .
suppose in a page I have a method that deletes an order :
protected void DeleteOrder(Order order)
{
Orders.Delete(order);
DeletedEvent(this,order);
}
then the subscriber uses the sent data to create the log . this class checks the object type and according to the type calls the right private method to create log using objects properties .
is this applicable in general ? if so , is this a good approach ? thanks in advance
Yes, you’re approach is good.
Is this a MVC.NET application? Take a look at this article:
http://weblogs.asp.net/shijuvarghese/archive/2011/07/18/user-activity-logging-in-asp-net-mvc-app-using-action-filter-and-log4net.aspx