gpt4 book ai didi

c# - 为什么 NLog 在记录大量消息时会漏掉一些消息?

转载 作者:太空狗 更新时间:2023-10-29 17:50:50 37 4
gpt4 key购买 nike

我尝试使用以下设置测试 NLog 性能(最新版本):

<?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">
<variable name="basePath" value="c:\logs\" />
<variable name="msgFormat" value="${message}" />
<targets async="true">
<target name="file"
xsi:type="File"
fileName="${basePath}/${logger}/${date:format=yyyy}/${date:format=MMMM}/log-${date:format=yyMMdd}-${level}.log"
layout="${msgFormat}"
concurrentWrites="true" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file"/>
</rules>
</nlog>

并运行这段代码:

var msg = "this is example string for logging test. it's not very long, but not very short";
var count = 20000;
Parallel.For(0, count, x => nlog.Info(msg));

NLog 写入文件,但当文件大小达到 1MB 时它停止写入。我尝试使用简单的 for 循环,但它对我没有帮助。我尝试使用 internal logging ,但没有错误,顺便说一句,我看到了这个字符串:

2013-04-01 11:36:18.2458 Trace Opening c:\logs/NLogTest/2013/April/log-130401-Info.log with concurrentWrite=False

很奇怪,因为concurrentWrites默认值为true,而且我已经在配置中设置了这个值。

最佳答案

问题出在AsyncWrapperQueueLimit的默认值,即10000。

该值决定了允许写入的消息队列有多大,问题出现是因为所有 20000 条消息在写入文件之前都已排队,这导致 NLog 丢弃最后的 10000 条消息。

不幸的是,当使用 async 属性时,这无法更改,您必须手动定义 AsyncWrapper 才能控制 QueueLimit,这是这样做的:

<?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">
<variable name="basePath" value="c:\logs\" />
<variable name="msgFormat" value="${message}" />
<targets async>
<target name="asyncWrapper" xsi:Type="AsyncWrapper" queueLimit="20000">
<target name="file"
xsi:type="File"
fileName="${basePath}/${logger}/${date:format=yyyy}/${date:format=MMMM}/log-${date:format=yyMMdd}-${level}.log"
layout="${msgFormat}"
concurrentWrites="true" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file"/>
</rules>
</nlog>

其中 QueueLimit 设置为 20000。

如果您需要做一些其他的事情,您也可以更改 OverflowAction 丢弃未放入队列的消息,请参阅 AsyncWrapper文档以获取更多信息。选项是 Block、Discard 或 Grow。

关于c# - 为什么 NLog 在记录大量消息时会漏掉一些消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15739591/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com