NLog-删除X天之前的日志 [英] NLog - delete logs older than X days

查看:279
本文介绍了NLog-删除X天之前的日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何删除具有X天以上日志的文件.很简单,但是我只有一天才在一个文件夹中保存日志.我的NLog.config看起来像:

How I can delete files with logs older than X days. It's simple, but I have in one folder logs only from one day. My NLog.config looks like:

<?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" autoReload="true">
  <extensions>
    <add assembly="NLog.Extended" />
  </extensions>
  <variable name="LogHome" value="PATH"/>
  <variable name="DailyDir" value="${LogHome}${date:format=yyyy}/${date:format=MM}/${date:format=dd}/"/>
  <targets>
    <target name="asyncFile" xsi:type="AsyncWrapper">
      <target
        name="fatalLog"
        xsi:type="File"
        layout="${longdate}|${callsite}|${message}|${exception}"
        fileName="${DailyDir}/Fatal.txt"
              />
    </target>
    <target name="asyncFile" xsi:type="AsyncWrapper">
      <target
        name="errorLog"
        xsi:type="File"
        layout="${longdate}|${callsite}|${message}|${exception}"
        fileName="${DailyDir}/Error.txt"
              />
    </target>
  </targets>
  <rules>
    <logger name="*" level="Fatal" writeTo="fatalLog" />
    <logger name="*" level="Error" writeTo="errorLog" />
  </rules>
</nlog>

推荐答案

现在,您正在包含日期的目录中创建日志.要使NLog能够自动管理您的当前和旧日志文件,您需要使用NLog存档功能.如NLog文件目标文档此处所述,您可以使用属性archiveFileNamemaxArchiveFiles以及每日日志,以在NLog删除它们之前将日志文件保留X天.

right now you are creating logs in directories containing the date. To enable NLog to automatically manage your current and old log files, you need to use the NLog archiving functionality. As documented in the NLog file target documentation here you can use the attributes archiveFileName and maxArchiveFiles together with a daily log to keep log files for X days before NLog removes them.

您可能必须将所有存档日志保存在一个目录中,否则NLog将无法找到较旧的日志并将其删除.我将创建一个存档目录作为您的主日志目录的子目录,让NLog将所有存档日志放在此处,然后仅使用maxArchiveFiles参数告诉NLog您要保留多少个日志.

You probably have to keep all archived logs in a single directory, otherwise NLog won't be able to locate the older logs and delete them. I would create an archive directory as a subdirectory of your main logging directory, have NLog put all archive logs there and then just use the maxArchiveFiles parameter to tell NLog how many of those logs you want to keep.

<targets>
<target name="asyncFile" xsi:type="AsyncWrapper">
  <target
    name="fatalLog"
    xsi:type="File"
    layout="${longdate}|${callsite}|${message}|${exception}"
    fileName="${LogHome}/Fatal.txt"
    archiveFileName="${LogHome}/Archive/Fatal-${shortdate}.txt"
    maxArchiveFiles="5"
    archiveEvery="Day"
          />
</target>
<target name="asyncFile" xsi:type="AsyncWrapper">
  <target
    name="errorLog"
    xsi:type="File"
    layout="${longdate}|${callsite}|${message}|${exception}"
    fileName="${LogHome}/Error.txt"
    archiveFileName="${LogHome}/Archive/Error-${shortdate}.txt"
    maxArchiveFiles="5"
    archiveEvery="Day"
          />
</target>
</targets>

应该为您提供两个带有当前日志的日志文件,以及一个归档目录,其中包含最近5天中每个目标的5个日志.

that should give you two log files with the current log and an archive directory with 5 logs for each target from the last 5 days.

这篇关于NLog-删除X天之前的日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆