NLog的介绍使用
先参考这个: https://www.cnblogs.com/zhangchengye/p/6297685.html
补充:
日志写到json文件中
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true"> <targets> <target name="jsonFile" xsi:type="File" fileName="${basedir}/${logger}/${level}/${date:format=yyyy-MM-dd}.json" archiveFileName="${basedir}/${logger}/${level}/log.{#}.json" archiveNumbering="DateAndSequence" archiveAboveSize="10483760" archiveDateFormat="yyyyMMdd" encoding="UTF-8"> <layout xsi:type="JsonLayout"> <attribute name="time" layout="${longdate}"/> <attribute name="machine" layout="${machinename}"/> <attribute name="level" layout="${level:upperCase=true}"/> <attribute name="message" layout="${message}" encode="true"/> </layout> </target> <target name="warningFile" xsi:type="File" fileName="${basedir}/warn/log.text" archiveFileName="${basedir}/${logger}/${level}/log.{#}.json" archiveNumbering="DateAndSequence" archiveAboveSize="10483760" archiveDateFormat="yyyyMMdd"> <layout xsi:type="JsonLayout"> <attribute name="time" layout="${longdate}"/> <attribute name="machine" layout="${machinename}"/> <attribute name="logger" layout="${logger}"/> <attribute name="level" layout="${level:upperCase=true}"/> <attribute name="message" layout="${message}"/> </layout> </target> <target name="fatalFile" xsi:type="File" fileName="${basedir}/fatal/log.text" archiveFileName="log.{#}.json" archiveNumbering="DateAndSequence" archiveAboveSize="10483760" archiveDateFormat="yyyyMMdd"> <layout xsi:type="JsonLayout"> <attribute name="time" layout="${longdate}"/> <attribute name="machine" layout="${machinename}"/> <attribute name="logger" layout="${logger}"/> <attribute name="message" layout="${message}"/> </layout> </target> </targets> <rules> <!--<logger name="测试" minlevel="Debug" maxlevel="Error" writeTo="nxlog_json"/> <logger name="bar" minlevel="Debug" maxlevel="Error" writeTo="jsonFile"/> <logger name="*" levels="Debug,Warn" writeTo="warningFile,jsonFile"/>--> <logger name="*" minlevel="Debug" maxlevel="Error" writeTo="jsonFile"/> </rules> </nlog>
注意:防止汉字转码 为了生成汉字信息:
换引用 nlog官方默认转义unicode字符 用我重新编译的 DLL NLog.4.4.8_enableUnicodeJson 参考项目:BidSolution 不懂的朋友留言给我!