首页 > 解决方案 > NLog - 文件轮换 - 序列问题

问题描述

我每天都使用 Nlog 来记录日志和旋转文件。日志文件由另一个系统进一步读取和处理以移动到 Splunk。

在这里,我希望找出 nlog 需要哪些步骤来旋转文件,因为它无法与其他系统集成。它适用于其他日志框架,如 log4net/logback。如果是这种情况,我可能需要切换到其他框架。

理想的步骤应该是 - 方法 1

 1. Open file - abc.txt 
 2. add log line 1, log line 2.. log line n. 
 3. 12.00 AM - file rotation - Rename file abc.txt => 2020-05-13 abc.txt 
 4. create new file - abc.txt 5. add log line a1, log line a2.. log line an.

另一种方法可能是 - 方法 2

 1. open file - abc.txt
 2. add log line 1, log line 2.. log line n.
 3. 12.00 AM - file rotation - create new file 2020-05-13 abc.txt
 4. move contents from abc.txt to new file 2020-05-13 abc.txt
 5. Update file offset/pointer to move to new location i.e. start of the file (as contents are removed from this file)
 6. add log line a1, log line a2.. log line an - at the begining location (offset/file pointer)

任何人都可以确认 Nlog 究竟执行了哪种方法或步骤序列来进行文件轮换。

[更新] 下面是我的配置的样子-

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <variable name="logDir" value="C:\logs">
    <targets>
        <target name="logFile" xsi:type="File" fileName="${logDir}\API_${machinename}.json"
        archiveFileName="${logDir}\{#}_API_${machinename}.json" archiveNumbering="Date"
        archiveDateFormat="yyyy-MM-dd" archiveEvery="Day">
            <layout xsi:type="JsonLayout" includeAllProperties="true">
                <attribute name="time" layout="${date:format=yyyy-MM-ddTHH\:mm\:ss.fffzzz}"/>
                <attribute name="level" layout="${level:upperCase=true}"/>
                <attribute name="message" layout="${message}"/>
                <attribute name="exception" layout="${exception:format=@}"/>
            </layout>
        </target>
    </targets>
    <rules>
        <logger name="*" minlevel="Debug" writeTo="logFile" />
    </rules>
 </nlog>

标签: nlog

解决方案


推荐阅读