I have to log different actions on site. For example, CRUD operations. It's not simple message: "Entity is saved". For different entity various parameters. For example, when changing user I have to save ID, personal number also, for business trip entity I have to save begin and end dates of business trip. This is my code for updating user:
public UserController : Controller
{
public ActionResult Update(...)
{
if (ModelState.IsValid)
{
...
var id = _repository.SaveOrUpdate(user);
_eventLogger.WriteEventLog(new EventLoggerData
{
EventType = EventLogType.Update,
ObjectType = ObjectType.User,
ObjectId = id,
EventData = _jsonObjectMapper.ToJson(new NumberedEventLoggerData
{
Number = number
})
});
}
}
}
All works fine. But the code :
_eventLogger.WriteEventLog(new EventLoggerData
{
EventType = EventLogType.Update,
ObjectType = ObjectType.User,
ObjectId = id,
EventData = _jsonObjectMapper.ToJson(new NumberedEventLoggerData
{
Number = number
})
});
is the big and this piece of the code will be repeated more than once. I have about 20 controllers and each with CRUD operations.
May be there are more flexible way?
Sorry for my English.