I have an audit table in SQL server. It is to record an entry for each high-level user action, e.g. update a record, add a new record, delete a record etc.
I have a mechanism for detecting and recording all the changes made (in .NET, not as a trigger in the database) and have a collection of objects that record a field name, previous value and new value. I’m wanting to store the field changes in the same table (not in a separate table, i.e. I don’t want a full-blown normalised relational design for this), so I have a blob field (or it could be a character field) that I want to record the field-level audit data in.
I’m thinking I just want to take my object graph (basically just a list of these field change objects) and serialize it and store the serialized version in the table.
Later, when the user wants to view the changes I can deserialize the field and reconstruct the collection of field changes.
So, what would be the best framework/serialization format in .NET 3.5 to use? I don’t much mind about the size, and it doesn’t have to be human-readable.
Avoid
BinaryFormatterfor anything that you store long-term (for example in a database); because it contains type/assembly metadata you can easily find that you can’t deserialize the data later. Plus it is .NET-specific! So you’re a bit scuppered if you want to read it from any other platform.JSON (via Json.NET) would make a simple, pretty readable format that doesn’t take much space. Xml via
XmlSerializerorDataContractSerializerwould be fine but isn’t as readable. If space is your biggest concern, perhaps something like “protocol buffers” (protobuf-net and others) – virtually impossible to read without the supporting utility dll, but very fast and efficient.I’d be tempted to use JSON, personally. It means I can read the audit in SSMS…