We currently have a requirement to implement audit trail generation for some of our main business entities, as usual in these cases we need to persist old and new value of every field which was changed and also some header data like time stamp, entity ID and user who did the save.
I understand there are different ways to do this, such as:
A .NET Reflection based method could possibly take a bit longer to be written but if properly done will be smart enough to include new properties added in the future without any code change, and it can also expand and compare all children entities (like collections of other sub-entities added to our main .NET entity).
We actually have a legacy application which uses such .NET based audit trail generation, we save the whole audit trail as XML field in a SQL database and over the years that audit table is now something like 35GB of data.
I am thinking how easy it could be a trigger based solution in terms of:
... and how about performances?
Does anybody have experience with both approaches and can suggest or point out some pros and cons?
In the past, for similar requirements, I have turned to domain events and messaging. It does bring in some complexity, but can be worth it. I would suggest at least considering it.
Essentially, you would make the change a first class citizen of the model by defining an event that fires when changes are made to the business objects. These events can also be a good way to capture business intent instead of just changes at the field level. For example, a business event named OrderRefunded
is usually a better audit point than OrderTotal field changed from 45.00 to 0.00
.
Firing these domain events with messaging using publish/subscribe allows many subscribers to handle the event. One of these subscribers could be an Audit subscriber. This takes all the performance impact (of rebuilding indexes etc) away from the domain that is processing the original request and places the burden on the Audit subscriber. this also means that you would never get a problem where a bug in your audit code takes down the processing of business transactions.
Another benefit is about how much data that needs to be saved. This approach gives you the advantage that the Audit subscriber will only have to save the amount of data that it intends to use. Rules about how much data to save or archive are also localized to the service that is handling audit. Therefore, you can be sure that you are not storing any data without a need.
Tools I've used in the past to do this include NServiceBus and RabbitMQ. Each had its benefits and liabilities depending on the problem.
Requirements for audit are very specific to each every organisation. In my last project, we were required to keep an audit trail on messages sent to Real time sysems.
The volume was substantial.. some days over 50GB text files and on others average 10-15GB.
The 1st solution we used was to persist it in SQL
About 2 years back
Last year
What you choose is based on your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With