NLog - 如何将自定义数据记录到 XML
您好,我需要将温度记录到 XML 文件。我想要这种格式的 XMl
<logs>
<log>
<dateTime></dateTime/>
<value>x</dateTime/>
<log>
<log>
<dateTime></dateTime/>
<value>x</dateTime/>
<log>
<log>
<dateTime></dateTime/>
<value>x</dateTime/>
<log>
</logs>
首先,我尝试使用以下配置将数据记录到 txt 文件:
<targets>
<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>
问题是该级别信息记录所有数据,我只需要记录自定义数据。例如,当我只想记录一些字符串时,我不知道必须使用哪种类型的级别。
第二个问题是如何创建XML布局?
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>
<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是 nlog 的预期目的。其目的是通过将信息写入一个或多个日志源来帮助您调试应用程序。
您需要使用某种 XML 序列化程序,它将温度变化写入文件。
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.Trace(...)、log.Debug(...)、log.Info(...)
实现不同级别的日志记录。XML 布局:查看
NLog.Layouts
和NLog.LayoutRenderers
。注意:这不是一个相对容易的任务,并且考虑到您无法弄清楚如何实现不同级别的日志记录,您可能需要推迟此任务并处理您的实际项目。编辑:您确定不能仅将记录的数据以简单格式输出到普通文本文件,然后在记录完成后解析数据并写入 XML 文件吗?
Log "custom data": Use
log.Trace(...), log.Debug(...), log.Info(...)
to achieve different levels of logging.XML Layout: Look into
NLog.Layouts
andNLog.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?