App活动监视器

时间:2016-03-14 06:23:55

标签: c# asp.net

需要记录我的MVC 5应用程序的几乎所有用户活动,即7个表的创建,更新和删除。

管理这些表的部分更新涉及近25种操作方法,我找到了一种记录所有这些操作方法的活动而无需在任何地方进行更改的方法。

有什么建议吗?如何实现这个目标?

1 个答案:

答案 0 :(得分:0)

我会启动一个后台记录工作者(在后台/并行任务中),并通过某种中介向它提交消息。

您必须保留一个可以发布/订阅的记录器,单例和一个中介的实例。

一个实现可能是使用Prism Event Aggregator:

public class Logger 
{
   private IEventAggregator _eventAggregator;

   public Logger(IEventAggregator eventAggregator){
      _eventAggregator = eventAggregator;

      // Register events to log
      _eventAggregator.GetEvent<Messages.MethodCalled>().Subscribe(() => LogMethodCall);
   }

   public void LogMethodCall(string information){
      // Do log things here
   }
}

这需要您在某处发布这些事件

public void DoSomething(){
   // other code

   // log it
   _eventAggregator.GetEvent<Messages.MethodCalled>().Publish("DoSomething called");
}

我希望你明白这个主意。这是一种相当干净,不显眼的登录后台任务的方式,因此您不会从主执行线程中窃取任何时间。