开发者

NLog - how logging custom data to XML

开发者 https://www.devze.com 2023-04-13 00:12 出处:网络
Hi I need log temperature to XML file. I would like have XMl in this format <logs> <log> <dateTime></dateTime/>

Hi I need log temperature to XML file. I would like have XMl in this format

<logs>
   <log>
     <dateTime></dateTime/>
     <value>x</dateTime/>
   <log>
   <log>
     <dateTime></dateTime/>
     <value>x</dateTime/>
   <log>
   <log>
     <dateTime></dateTime/>
     <value>x</dateTime/>
   <log>
</logs>

First I tried log data to txt file with this configuration:

<targets>
  &开发者_运维问答lt;target name="logfile" xsi:type="File" fileName="users.log" layout="${date:format=yyyyMMddHHmmss} ${message}" />
  <target name ="xmlFile" xsi:type="File" fileName="log.xml"/>
</targets>

<rules>
  <logger name="*" minlevel="Info"  writeTo="logFile" /> 
</rules>

Problems is that level Info log all data I need log only custom data. I dont know which type of level I must use when I want log only some string for example.

Second problem is how create XML layout?


That's not the intended purpose of nlog. It's purpose is to help you debugging your application by writing information to one or more log sources.

What you need to use is some sort of XML serializer which will write the tempature changes to a file.


Log "custom data": Use log.Trace(...), log.Debug(...), log.Info(...) to achieve different levels of logging.

XML Layout: Look into NLog.Layouts and NLog.LayoutRenderers. Note: this isn't a relatively easy task, and considering you couldn't figure out how to achieve different levels of logging, you may want to delay this task and work on your actual project.

Edit: Are you sure you can't just output the logged data to a normal text file in simple format, and when logging is complete, parse the data and write to an XML file?

0

精彩评论

暂无评论...
验证码 换一张
取 消