c# - log4net - Append errors to an xml file -
i trying append error xml file, not able append way wanted.
i using custom layout overriding xmllayout format method shown below.
public class myxmllayout : log4net.layout.xmllayout { public static bool isfirsttime = true; protected override void formatxml(system.xml.xmlwriter writer, loggingevent loggingevent) { if (isfirsttime) { writer.writestartdocument(); writer.writestartelement("exceptions"); } writer.writestartelement("exception"); writer.writestartelement("error"); writer.writeattributestring("date", loggingevent.timestamp.touniversaltime().tostring()); writer.writeattributestring("user", loggingevent.username); writer.writestring(loggingevent.renderedmessage); writer.writeendelement(); writer.writeendelement(); if (isfirsttime) { writer.writeendelement(); writer.writeenddocument(); isfirsttime = false; } } }
yes, appending through above code, problem unable read xml file not in proper format.
the generated xml above code looks like
<?xml version="1.0" encoding="windows-1252"?> <exceptions> <exception> <error date="11-11-2014 13:47:53" user="sourceedge sunilkumar">exception 1</error> </exception> </exceptions> <?xml version="1.0" encoding="windows-1252"?> <exceptions> <exception> <error date="11-11-2014 14:01:44" user="sourceedge\sunilkumar">exception 2</error> </exception> </exceptions>
and should
<?xml version="1.0" encoding="windows-1252"?> <exceptions> <exception> <error date="11-11-2014 13:47:53" user="sourceedge\sunilkumar">exception 1</error> </exception> <exception> <error date="11-11-2014 14:01:44" user="sourceedge\sunilkumar">exception 2</error> </exception> </exceptions>
please solution.
updated per comment.
its ending exceptions tag prematurely because told
if (isfirsttime) { writer.writestartdocument(); writer.writestartelement("exceptions"); }
and
if (isfirsttime) { writer.writeendelement(); }
rearrange code writer starts xml document , exceptions element when file opened , ends before file closed.
a quick google suggests these should go overrides header , footer respectively, instead of in override format have.
this link vb, gives idea of how want:
http://blogs.lessthandot.com/index.php/desktopdev/mstech/making-an-xmllayout-for-log4net/
Comments
Post a Comment