使用NLOG作为一个过渡的文件记录仪 [英] Using NLog as a rollover file logger

查看:261
本文介绍了使用NLOG作为一个过渡的文件记录仪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何 - 如果可能的话 - 我可以使用NLOG作为一个过渡的文件记录?就好像:

How - if possible - can I use NLog as a rollover file logger? as if:

我想最多有31档31天,当新的一天开始了,如果有一个古老的一天的日志文件##。登录,那么它应该被删除,但在这一天所有的日志记录在和会有至少27天

I want to have at most 31 files for 31 days and when a new day started, if there is an old day log file ##.log, then it should be deleted but during that day all logs are appended and will be there at least for 27 days.

推荐答案

最后,我已经解决用的基于大小的文件归档。我用了一招文件月份就在一天后的名字,我所需要的尺寸,基于文件的归档,因为它确实有助于当你的日志开始增长超过几百兆字节。它有助于让 - 例如 - 日志20 MB的数据块,因此可以很容易地快速浏览一下它具有重量轻工具如记事本+ +

Finally I have settled with size-based file archival. I use a trick to name the file after just the day of month and I needed the size-based file archival because it really helps when you logs begin to grow beyond some hundred mega-bytes. It helps to make - for example - 20 MB chunks of log, so one can easily take a quick look at it with a light weight tool like Notepad++.

这是工作了将近一年了。这是我的 NLog.config 文件的一个简化版本:

It is working for almost a year now. Here is a simplified version of my NLog.config file:

<?xml version="1.0" encoding="utf-8" ?>
<nlog autoReload="true" throwExceptions="true"
      xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <variable name="LogDir" value="${specialfolder:folder=MyDocuments}/MyApp/Log"/>
  <variable name="LogDay" value="${date:format=dd}"/>
  <targets>
    <target name="LogTarget1" xsi:type="File" fileName="${LogDir}/${LogDay}.log" encoding="utf-8"
        maxArchiveFiles="10" archiveNumbering="Sequence" archiveAboveSize="1048576" archiveFileName="${LogDir}/{#######}.a" />
  </targets>
  <rules>
    <logger name="AppLog" writeTo="LogTarget1" />
  </rules>
</nlog>

该配置使得1 MB的日志文件月份的每一天,并保持在10存档的1 MB的日志块在我的文档\ MyApp的\登录文件夹;如 29.log 30.log 31.log

This config makes 1 MB log file for each day of month and keep at most 10 archived 1 MB log chunks in My Documents\MyApp\Log folder; like 29.log, 30.log and 31.log.

编辑:这是一段时间以来,我用这个 NLog.config 文件,它涵盖了pretty的多,我需要每例。我从不同类别在不同的文件和不同级别的日志记录时,他们已经得到了很大的,他们将获得根据大小存档,在一个小时的方式:

It's for some time that I use this NLog.config file and it covers pretty much every cases that I need. I have different levels of logging from different classes in separate files and when they've got big, they will get archived based on size, in a hourly manner:

<?xml version="1.0" encoding="utf-8" ?>
<nlog autoReload="true" throwExceptions="true" internalLogFile="nlog-internals.log"
      xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <variable name="LogHome" value="${basedir}/Log"/>
  <variable name="DailyDir" value="${LogHome}/${date:format=yyyy}/${date:format=MM}/${date:format=dd}"/>
  <variable name="HourlyArchive" value="${DailyDir}/${date:format=HH}-Archive/${level}-${logger}-{#######}-archived.a"/>
  <variable name="AppLogPath" value="${DailyDir}/${level}-${logger}.log"/>
  <variable name="DataLogPath" value="${DailyDir}/_data/inouts-${shortdate}.log"/>
  <variable name="EventSource" value="Application" />
  <targets>
    <target name="AppAsyncTarget" xsi:type="AsyncWrapper">
      <target xsi:type="RetryingWrapper" retryDelayMilliseconds="3000" retryCount="10">
        <target xsi:type="File" fileName="${AppLogPath}" encoding="utf-8"
            maxArchiveFiles="50" archiveNumbering="Sequence" archiveAboveSize="1048576" archiveFileName="${HourlyArchive}"
            layout="`${longdate}`${level}`${message}" />
      </target>
    </target>
    <target name="DataAsyncTarget" xsi:type="AsyncWrapper">
      <target xsi:type="RetryingWrapper" retryDelayMilliseconds="1500" retryCount="300">
        <target xsi:type="File" fileName="${DataLogPath}" encoding="utf-8"
            layout="`${longdate}`${message}" />
      </target>
    </target>
    <target name="EventLogAsyncTarget" xsi:type="AsyncWrapper">
      <target xsi:type="RetryingWrapper">
        <target xsi:type="EventLog" source="${EventSource}" machineName="." />
      </target>
    </target>
  </targets>
  <rules>
    <logger name="Data" writeTo="DataAsyncTarget" final="true" />
    <logger name="Event" writeTo="EventLogAsyncTarget" final="true" />
    <logger name="*" writeTo="AppAsyncTarget" />
  </rules>
</nlog>

而在这我想记录功能每个类,我把这样的:

And in each class that I want a logging functionality, I put this:

static readonly Logger SlotClassLogger = LogManager.GetCurrentClassLogger();
static Logger ClassLogger { get { return SlotClassLogger; } }

另外两个记录仪是用于堆放一些数据每天和写入到Windows事件日志;这是应用程序范围内的记录器:

Two additional loggers are for piling some data on a daily basis and writing to Windows Event Log; which are app-wide loggers:

public static Logger DataLog { get; private set; }
public static Logger AppEventLog { get; private set; }

和他们应该在初始化程序启动:

And they should be initialize at app start:

DataLog = LogManager.GetLogger("Data");
AppEventLog = LogManager.GetLogger("Event");

请注意:有时你的应用程序退出你通过NLOG产生的异常。这是因为一些没有初始化,无法得到处置!你只写一个空的进入你的记录在应用程序启动,说:

Note: Sometimes on you app exit you get an exception produced by NLog. It's because something that is not initialized, can not get disposed! You have just write an empty entry into your logger at app start, say:

DataLog.Info(string.Empty);

我已经加入这个尺寸限制,所以日志文件可以在低端服务器上(比如说)记事本查看,快速审查。你应该根据你的需要进行修改。

I have added this size limitation so log file can be viewed in (say) Notepad on a low-end server, for quick reviews. You should modify them based on your needs.

这篇关于使用NLOG作为一个过渡的文件记录仪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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