NLog日志循环删除

19 篇文章 0 订阅

参考官方配置:File target · NLog/NLog Wiki · GitHub

NLog版本:4.0.0.0

.Net Framework:4.0

(1)每分钟归档一次,最多保留3个文件

<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
  <variable name="myvar" value="myvalue"/>
  <targets>
    <target name="logconsole" xsi:type="Console" />
    <!--保存至文件-->
    <target name="logfile" xsi:type="File"  encoding="utf-8"
       fileName="${basedir}/AppLogs/${date:format=yyyy}/${date:format=MM}/${shortdate}.txt"
       archiveFileName="${basedir}/AppLogs/Archive/${shortdate}.{####}.txt"
       maxArchiveFiles="3"
       archiveEvery="Minute"
       layout="RecordTime:${longdate}    ThreadID:${threadid}    Level:${level}${newline}LogMessage:${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
  </targets>
  <rules>
    <!--minlevel 大于等于当前级别的都会被记录下来-->
    <logger name="*" minlevel="Debug" writeTo="logfile" />
  </rules>
</nlog>

注意:

在 NLog 4.5 之前,如下截图中存档文件名中如果没有占位符,存档时NLog会把Archive目录下的所有其他文件删除,只保留3条,如下截图二

图一

 

 图二

(2)每天归档一次,最多保留30个文件

<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
  <variable name="myvar" value="myvalue"/>
  <targets>
    <target name="logconsole" xsi:type="Console" />
    <!--保存至文件-->
    <target name="logfile" xsi:type="File"  encoding="utf-8"
            fileName="${basedir}/AppLogs/logfile.txt"
            archiveFileName="${basedir}/AppLogs/Archive/Log{######}.txt"
            maxArchiveFiles="30"
            archiveEvery="Day"
            layout="RecordTime:${longdate}  ThreadID:${threadid}  Level:${level}${newline}LogMessage:${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
    
  </targets>
  <rules>
    <!--minlevel 大于等于当前级别的都会被记录下来-->
    <logger name="*" minlevel="Debug" writeTo="logfile" />
  </rules>
</nlog>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值