NLog 缺少具有异步滚动更新文件配置的日志条目
NLog missing log entries with async rollover file configuration
NLog 没有使用异步滚动文件记录器配置记录每条日志消息。
以下是我的NLog.configxml.
<?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" >
<targets async="true">
<target name="logfile" xsi:type="File" fileName="logs/log.txt"
maxArchiveFiles="10"
archiveAboveSize="1048576"
archiveNumbering="Sequence"
concurrentWrites="true"
archiveFileName="logs/log.{####}.txt"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
下面是我的代码。
private static Logger log = LogManager.GetCurrentClassLogger();
Stopwatch sw = new Stopwatch();
public Form1()
{
InitializeComponent();
LogManager.ReconfigExistingLoggers();
}
private void button1_Click(object sender, EventArgs e)
{
string toLog;
sw.Start();
for (int i = 0; i < 100000; i++)
{
toLog = "Message:" + i.ToString();
log.Debug(toLog);
}
sw.Stop();
string s = "STOP :" + sw.Elapsed.ToString();
log.Debug(s);
}
结果我只得到一个文件,并且缺少一些日志条目。以下是生成的 log.txt 文件。
2015-10-16 12:04:08.9865|DEBUG|NLogAsync.Form1|Message:11591
2015-10-16 12:04:08.9865|DEBUG|NLogAsync.Form1|Message:11592
2015-10-16 12:04:08.9865|DEBUG|NLogAsync.Form1|Message:11593
2015-10-16 12:04:08.9865|DEBUG|NLogAsync.Form1|Message:11594
2015-10-16 12:04:09.0802|DEBUG|NLogAsync.Form1|Message:80436
2015-10-16 12:04:09.0802|DEBUG|NLogAsync.Form1|Message:80437
2015-10-16 12:04:09.0802|DEBUG|NLogAsync.Form1|Message:80438
2015-10-16 12:04:09.0802|DEBUG|NLogAsync.Form1|Message:80439
然而,当我删除异步属性时,代码 运行 正确。
<targets async="true">
只是一个 shorthand,用于用 AsyncWrapper 目标包装所有目标。 IE。您的配置相当于:
<targets>
<target name="logfile" xsi:type="AsyncWrapper">
<target xsi:type="File" fileName="logs/log.txt"
maxArchiveFiles="10"
archiveAboveSize="1048576"
archiveNumbering="Sequence"
concurrentWrites="true"
archiveFileName="logs/log.{####}.txt"/>
</target>
</targets>
此异步包装器将使用默认设置。对您来说最重要的是 queueLimit
默认为 10000,而 overflowAction
默认为 Discard
。这是什么意思?当写入线程在队列中有超过 10000 条日志消息后,所有新的日志消息都将被丢弃。确保处理所有日志消息的最简单方法是将默认的 overflowAction 值更改为 Grow
。但是您需要为此手动指定 AsyncWrapper
:
<targets>
<target name="logfile" xsi:type="AsyncWrapper" overflowAction="Grow">
<target xsi:type="File" fileName="logs/log.txt"
maxArchiveFiles="10"
archiveAboveSize="1048576"
archiveNumbering="Sequence"
concurrentWrites="true"
archiveFileName="logs/log.{####}.txt"/>
</target>
</targets>
NLog 没有使用异步滚动文件记录器配置记录每条日志消息。
以下是我的NLog.configxml.
<?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" >
<targets async="true">
<target name="logfile" xsi:type="File" fileName="logs/log.txt"
maxArchiveFiles="10"
archiveAboveSize="1048576"
archiveNumbering="Sequence"
concurrentWrites="true"
archiveFileName="logs/log.{####}.txt"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
下面是我的代码。
private static Logger log = LogManager.GetCurrentClassLogger();
Stopwatch sw = new Stopwatch();
public Form1()
{
InitializeComponent();
LogManager.ReconfigExistingLoggers();
}
private void button1_Click(object sender, EventArgs e)
{
string toLog;
sw.Start();
for (int i = 0; i < 100000; i++)
{
toLog = "Message:" + i.ToString();
log.Debug(toLog);
}
sw.Stop();
string s = "STOP :" + sw.Elapsed.ToString();
log.Debug(s);
}
结果我只得到一个文件,并且缺少一些日志条目。以下是生成的 log.txt 文件。
2015-10-16 12:04:08.9865|DEBUG|NLogAsync.Form1|Message:11591
2015-10-16 12:04:08.9865|DEBUG|NLogAsync.Form1|Message:11592
2015-10-16 12:04:08.9865|DEBUG|NLogAsync.Form1|Message:11593
2015-10-16 12:04:08.9865|DEBUG|NLogAsync.Form1|Message:11594
2015-10-16 12:04:09.0802|DEBUG|NLogAsync.Form1|Message:80436
2015-10-16 12:04:09.0802|DEBUG|NLogAsync.Form1|Message:80437
2015-10-16 12:04:09.0802|DEBUG|NLogAsync.Form1|Message:80438
2015-10-16 12:04:09.0802|DEBUG|NLogAsync.Form1|Message:80439
然而,当我删除异步属性时,代码 运行 正确。
<targets async="true">
只是一个 shorthand,用于用 AsyncWrapper 目标包装所有目标。 IE。您的配置相当于:
<targets>
<target name="logfile" xsi:type="AsyncWrapper">
<target xsi:type="File" fileName="logs/log.txt"
maxArchiveFiles="10"
archiveAboveSize="1048576"
archiveNumbering="Sequence"
concurrentWrites="true"
archiveFileName="logs/log.{####}.txt"/>
</target>
</targets>
此异步包装器将使用默认设置。对您来说最重要的是 queueLimit
默认为 10000,而 overflowAction
默认为 Discard
。这是什么意思?当写入线程在队列中有超过 10000 条日志消息后,所有新的日志消息都将被丢弃。确保处理所有日志消息的最简单方法是将默认的 overflowAction 值更改为 Grow
。但是您需要为此手动指定 AsyncWrapper
:
<targets>
<target name="logfile" xsi:type="AsyncWrapper" overflowAction="Grow">
<target xsi:type="File" fileName="logs/log.txt"
maxArchiveFiles="10"
archiveAboveSize="1048576"
archiveNumbering="Sequence"
concurrentWrites="true"
archiveFileName="logs/log.{####}.txt"/>
</target>
</targets>