c# - serialize objects or collections to log -
i want save addtitional info error message. example should user query, or else. how should it?
is there build methods logging collections, structurest or objects? or should serialize myself?
no, there nothing built-in serializing objects. when use formatted methods debug<t>(string message, t argument)
internally (you can see class nlog.logeventinfo
) simple string.format
used creating formatted message (i.e. tostring()
called on every parameter).
i use json.net serializing objects , collections json. it's easy create extension method like
public static string tojson(this object value) { var settings = new jsonserializersettings { referenceloophandling = referenceloophandling.ignore }; return jsonconvert.serializeobject(value, formatting.indented, settings); }
and use during logging:
logger.debug("saving person {0}", person.tojson());
Comments
Post a Comment