Friday, 15 March 2013

c# - How to keep XmlWriterTraceListener from xml-encoding characters in the message -



c# - How to keep XmlWriterTraceListener from xml-encoding characters in the message -

i trying utilize system.diagnostics.xmlwritertracelistener log local file. start creating source , adding new xmlwritertracelistener it, so:

public class logger { const string log_err_fmt = "<message><![cdata[{0}]]></message>{2}"; tracesource tsource = new tracesource( "myapp", sourcelevels.verbose | sourcelevels.activitytracing ); public logger() { tsource.listeners.add( new xmlwritertracelistener("c:\\app_path\\app_tracelog.svclog", "locallistener") ); } public static logger deflogger = new logger(); public static logger default { {return deflogger; } set { if (value != null) deflogger = value; } } public void logerror(string msg, string extraxmldata) { tsource.traceevent(traceeventtype.error, 0, log_err_fmt, logmsg, extraxmldata); } }

i've simplified log format in sample above. actual logerror method takes exception object , inserts xml-serialized info message, that's not needed show problem here.

anyways, other part of app can log error "app_tracelog.svclog" file simple line like, oh say...

logger.default.logerror( "error parsing response web service \"someservice.com\".", "<requestbody><![cdata[" + responsebody + "]]></requestbody>" );

all until open app_tracelog.svclog. first of all, when seek open microsoft service trace viewer, programme behaves it's empty, , gives me message of "there no trace loaded file."

second, open file old notepad++ , find app did indeed log it. select , tell xmltools pretty up, , tool tells me can't because there's errors in xml.

so, start formatting manually. goes @ first, until actual application message logged. here, find stupid @#$ class has screwed nested xml replacing brackets , such encoded xml/html entities!

all '<'s have become "&lt;", '>'s have become "&gt;", etc... log message should have looked like:

<message><![cdata[error parsing response web service "someservice.com".]]></message><requestbody...

now looks like:

&lt;message&gt;&lt;![cdata[error parsing response web service "someservice.com".]]&gt;&lt;/message&gt;&lt;requestbody...

so big question is: how can forcefulness xmlwritertracelistener stop trashing message when log? know i'm doing when set in special xml characters, , thinks knows better! --or- is there listener class log local xml file way want?

c# xml tracelistener xml-encoding

No comments:

Post a Comment