Strategies for serializing an object for auditing/logging purpose in .NET?
Posted
by Jiho Han
on Stack Overflow
See other posts from Stack Overflow
or by Jiho Han
Published on 2010-06-09T17:31:57Z
Indexed on
2010/06/12
3:43 UTC
Read the original article
Hit count: 382
Let's say I have an application that processes messages. Messages are just objects in this case that implements IMessage interface which is just a marker.
In this app, if a message fails to process, then I want to log it, first of all for auditing and troubleshooting purposes. Secondly I might want to use it for re-processing.
Ideally, I want the message to be serialized into a format that is human-readable. The first candidate is XML although there are others like JSON. If I were to serialize the messages as XML, I want to know whether the message object is XML-serializable. One way is to reflect on the type and to see if it has a parameter-less constructor and the other is to require IXmlSerializable interface. I'm not too happy with either of these approaches. There is a third option which is to try to serialize it and catch exceptions. This doesn't really help - I want to, in some way, stipulate that IMessage (or a derived type) should be xml-serializable.
The reflection route has obvious disadvantages such as security, performance, etc. IXmlSerializable route locks down my messages to one format, when in the future, I might want to change the serialization format to be JSON. The other thing is even the simplest objects now must implement ReadXml and WriteXml methods.
Is there a route that involves the least amount of work that lets me serialize an arbitrary object (as long as it implements the marker interface) into XML but not lock future messages into XML?
© Stack Overflow or respective owner