Nlog 笔记

1. NuGet安装Nlog

2. 程序根目录创建NLog.config文件,属性修改为始终复制,下面内容贴进去保存

<?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" >
  <targets async="true" maxarchivefiles="2">
    <target  name="log_file" xsi:type="File"  fileName="${basedir}/logs/${date:format=yyyyMMdd}.log"
            layout="[============================${newline}${date} ${appdomain}  ${logger} ${level}${newline}${message}${newline}${exception}]"/>
  </targets>
  <rules>
    <logger name="*" minlevel="trace"  writeTo="log_file"></logger>
  </rules>
</nlog>

3. 定义

 private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

4. 日志写入

Logger.Error(ex.Message);
Logger.Info("test");
........

 

posted @ 2021-07-30 13:57  妖言惑众'  阅读(36)  评论(0)    收藏  举报