我的主要目标是使用NLog编写然后阅读以下信息。我做了以下但未能继续前进,
NLOG配置,
num = -1
counter = 1
anyNumber = int(raw_input("Enter any number: "))
while anyNumber > num:
anyNumber = int(raw_input("Enter another number: "))
counter += anyNumber
answer = counter + anyNumber
print answer
print "Good bye!"
这就是我想要读写这个文件的内容,
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="eventlog" xsi:type="EventLog" layout="${message}" log="Application" source="My App" />
<target name='jsonFile' type='File' fileName='${basedir}/APILog/${date:format=yyyy-MM-dd}-webapi.json'>
<layout type='JsonLayout'>
<attribute name='time' layout='${longdate}' />
<attribute name='executedby' layout='${executedby}' />
<attribute name='username' layout='${username}' />
<attribute name='deletedFromDB' layout='${deletedFromDB}' />
<attribute name='deletedFromAD' layout='${deletedFromAD}' />
</layout>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="eventlog" />
<logger name="*" minlevel="Trace" writeTo="jsonFile" />
</rules>
</nlog>
代码
var entryJson = Newtonsoft.Json.JsonConvert.SerializeObject(entry);
//看起来像这个
&#34; {\&#34;时间\&#34;:\&#34; 2018-03-02T11:33:55.426732 + 00:00 \&#34; \&#34; ExecutedBy \&#34;:\&#34; REMOVED \&#34; \&#34;用户名\&#34;:\&#34; EXAMPLEUSERNAME \&#34; \&#34; ExistingQAFromDB \& #34;:\&#34; 0 \&#34; \&#34; ExistingQAFromAD \&#34;:\&#34;假\&#34; \&#34; DeletedFromDB \&#34 ;:\&#34;真\&#34; \&#34; DeletedFromAD \&#34;:\&#34;假\&#34; \&#34;状态\&#34 ;:空}&#34 ;;
_logger.Debug(entryJson);
结果是......
{&#34; Time&#34;:&#34; 2018-03-02 11:34:01.1948&#34; }
如何读取和写入此LogEntry对象到日志文件?
答案 0 :(得分:1)
您已经在向NLog编写纯JSON了。无需激活NLog JsonLayout。
这样做:
<target name='jsonFile' type='File' fileName='${basedir}/APILog/${date:format=yyyy-MM-dd}-webapi.json' layout='${message}' />