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

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -