Friday, 15 March 2013

c# - NLog: How do I control the format of a message from a LogEventInfo object? -



c# - NLog: How do I control the format of a message from a LogEventInfo object? -

i'm new nlog, , i've been playing logeventinfo objects, since think i'll need them in application. created logeventinfo object plain text string, , called logger.debug(myeventinfoobject), using fileappender set write ${message}. expected see text string, saw instead logger name, message level, message, , sequence id. found that expanded string when phone call myeventinfoobject.tostring(). how can command appears when ${message} comes logeventinfo object?

here's config file:

<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/nlog.xsd" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" throwexceptions="true"> <targets> <target name="file" xsi:type="file" layout="${longdate} ${logger} ${message}" filename="${basedir}/nlog_sample_file.txt" /> </targets> <rules> <logger name="*" minlevel="debug" writeto="file" /> </rules> </nlog>

here's code generating log:

using system; using system.collections.generic; using system.linq; using system.text; using nlog; namespace nlogsample { class programme { static void main(string[] args) { seek { logger logger = logmanager.getcurrentclasslogger(); logeventinfo theevent = new logeventinfo(loglevel.debug, "", "this message loginfoevent not used."); theevent.properties["message"] = "this message loginfoevent message property used."; logger.debug(theevent); logger.debug("does sequence id show in message?"); string formattedmessage = theevent.formattedmessage; string eventstring = theevent.tostring(); } grab (exception ex) { int = 1; } } } }

finally, here sample of message:

2014-10-08 10:14:13.5525 nlogsample.program log event: logger='' level=debug message='this message loginfoevent not used.' sequenceid=2

i working in visual studio 2012 on win7 pro machine.

thanks much!

robr

if want create logeventinfo objects , fill them own information, should utilize 1 of logger.log method signatures, not logger.info/logger.debug/etc method signatures.

the behavior seeing right because logger.debug method receiving object (which happens logeventinfo object) , calling tostring on before logging it.

c# nlog

No comments:

Post a Comment