Auditing
Introduction
The process of auditing the Add, Change and Delete processes is based on the following events generated by the Mib repository:
- AfterCreate
- AfterDelete
- AfterSave
- BeforeDelete ⚠️
unused - BeforeSave ⚠️
unused
A Listener is required to process these events. To do this, we have provided two standard Listeners for you to use. They will be explained below.
How do I configure MibApi to log events?
Each time a record is changed (Add, Change or Delete), an event informing a Listener of the change is triggered. This Listener is responsible for persisting events. We currently have two standard listeners:
- MibEditHistoryListener
- MibEditHistoryListenerBatch
Every listener uses the MibObjectNotificationConfig for operation.
MibEditHistoryListener
Implements the IRepositoryNotificationListener interface responsible for providing the data needed to persist the audit event. Events are persisted individually with each save call (add, change, delete). This listener is available together with the EditHistoryMicroService project and needs it to work. see more about EditHistory
It is configured as follows:
MIBOBJECTNOTIFICATIONCONFIG_DEFAULT_NOTIFICATIONASSEMBLYNAME=MediaiBox.Cms.MicroServices.EditHistory.Listener
MIBOBJECTNOTIFICATIONCONFIG_DEFAULT_NOTIFICATIONCLASSFULLNAME=MediaiBox.Cms.MicroServices.EditHistory.Listener.MibEditHistoryListener
MibEditHistoryListenerBatch
Implements the RepositoryNotificationListenerBase abstract class responsible for providing the data needed to persist the audit event. This base class has batch persistence behavior. This listener is available together with the EditHistoryMicroService project and needs it to work. see more about EditHistory
It is configured as follows:
MIBOBJECTNOTIFICATIONCONFIG_DEFAULT_NOTIFICATIONASSEMBLYNAME=MediaiBox.Cms.MicroServices.EditHistory.Listener
MIBOBJECTNOTIFICATIONCONFIG_DEFAULT_NOTIFICATIONCLASSFULLNAME=MediaiBox.Cms.MicroServices.EditHistory.Listener.MibEditHistoryListenerBatch
MIBOBJECTNOTIFICATIONCONFIG_DEFAULT_MAXIMUMEVENTSTOBEPROCESS=10
MIBOBJECTNOTIFICATIONCONFIG_DEFAULT_INTERVALTOCOLLECTEVENTS=60
MIBOBJECTNOTIFICATIONCONFIG_DEFAULT_MAXIMUMPROCESSINGTIME=00:00:30
Can the listener be personalized?
Yes. To do this, you need to create a class that implements the IRepositoryNotificationListener (for persisting events as a unit) or the RepositoryNotificationListenerBase (for persisting events in batches). In the settings, simply enter your listener information: Example:
MIBOBJECTNOTIFICATIONCONFIG_DEFAULT_NOTIFICATIONASSEMBLYNAME=MyProject.Listener
MIBOBJECTNOTIFICATIONCONFIG_DEFAULT_NOTIFICATIONCLASSFULLNAME=MyProject.Listener.MyCustomListener
MIBOBJECTNOTIFICATIONCONFIG_DEFAULT_MAXIMUMEVENTSTOBEPROCESS=10
MIBOBJECTNOTIFICATIONCONFIG_DEFAULT_INTERVALTOCOLLECTEVENTS=60
MIBOBJECTNOTIFICATIONCONFIG_DEFAULT_MAXIMUMPROCESSINGTIME=00:00:30
Bulk operations
$bulkcreate and $bulkdelete (see Simple Operations, 8.1 and 13.1) raise the same events as the per-entity operations: an AfterCreate for every created entity, and a BeforeDelete and an AfterDelete for every deleted one. How a listener receives them depends on what it implements:
- A listener that implements
IRepositoryBulkNotificationListener(MediaiBox.Core.NotificationListener, an extension ofIRepositoryNotificationListener) gets one call per group of entities instead of one per entity, with the media type, the user and the list of items (id and object):AfterCreateMany,BeforeDeleteManyandAfterDeleteMany. That lets it persist a large operation in a few statements.BeforeSaveManyandAfterSaveManyare part of the interface, but no bulk operation raises them yet: relationships removed while deleting still arrive as per-entity BeforeSave / AfterSave events. - A listener that does not implement it gets one event per entity, exactly as for the per-entity operations.
RepositoryNotificationListenerBaseimplements it by queuing each item as its per-entity methods do, soMibEditHistoryListenerBatchand the listeners derived from it keep their current behaviour.SimpleAuditListener(MediaiBox.Cms.Api.Audit) writes the sameADM_AUDITrows, one per entity, in multi-row inserts. A subclass that overridesLogChangeskeeps getting one call per entity.
As with a per-entity event, an exception thrown by the listener is logged and does not fail the operation. In a bulk call it costs the whole group: that listener persists nothing for the entities of that call.
Delivery after the commit of a $batch
A listener that also implements IRepositoryPostCommitNotificationListener (MediaiBox.Core.NotificationListener) receives the AfterCreate, AfterSave and AfterDelete events of a $batch only once its transaction commits:
- If the batch is rolled back (a refused part, an ETag conflict, an error between parts), its events are never delivered, so no audit entry is written for changes that were not saved.
- The events are delivered in the order they were raised. A listener that implements
IRepositoryBulkNotificationListenerreceives them grouped, as for the bulk operations. - The object received is a snapshot of the changes taken when the event was raised, because the repository clears them right after its save.
- BeforeSave and BeforeDelete are never deferred. Calls made outside a
$batchkeep being notified at once.
MibEditHistoryListener and MibEditHistoryListenerBatch implement it. A listener that writes through IRepositoryListenerObject.ReferenceSql must not implement it, because by the time the events are delivered the transaction of that connection is over. This is the case of SimpleAuditListener, which keeps writing its ADM_AUDIT rows inside the transaction. The deferral is controlled by MediaiBox.Core.Objects.MibNotificationScope, which the MibApi $batch opens at its start and completes after its commit.
For more information on how to view the events on the front, click here