简体   繁体   English

NLog File目标和keepFileOpen标志

[英]NLog File target and keepFileOpen flag

A FileTarget object in NLog has a property (bool) called "keepFileOpen". NLog中的FileTarget对象具有名为“keepFileOpen”的属性(bool)。 By default this property is set to false, meaning that each log event will open the file, write in the file and close the file. 默认情况下,此属性设置为false,这意味着每个日志事件将打开文件,写入文件并关闭文件。

The hit in performance is huge, so I specified "keepFileOpen" to true, instead of false, meaning that the file will be opened only once. 性能上的打击是巨大的,所以我将“keepFileOpen”指定为true而不是false,这意味着该文件只会打开一次。

Does anybody has an idea (or just know) why this property is by default set to "false", incuring a HUGE performance hit on logging ? 是否有人有想法(或只知道)为什么默认情况下将此属性设置为“false”,从而在日志记录中产生巨大的性能损失?

Is there any scenarios where setting this property to true can cause issues (which would therefore make sense to default it to "false"). 是否存在将此属性设置为true的情况会导致问题(因此将其默认为“false”是有意义的)。

Thanks ! 谢谢 !

EDIT 编辑

Performance measurements for basic layout, writing 100K events to the logger : 基本布局的性能测量,将100K事件写入记录器:

  • keepFileOpen = false (default) : ~ 101 sec keepFileOpen = false(默认值):~101秒
  • keepFileOpen = true : ~ 1 sec keepFileOpen = true:~1秒

I would say that closing the file is the expected behaviour. 我会说关闭文件是预期的行为。 If you will try to access the file from another process or delete it in file system while the NLog-process is alive it would lead to those annoying system-errors saying that some process holds the file etc. 如果您尝试从另一个进程访问该文件或在NLog进程处于活动状态时将其删除到文件系统中,则会导致那些令人讨厌的系统错误,说某些进程保存文件等。

If the time of opening a file is too long for you, try using the AsyncWrapper and you'll get a fire-and-forget-behavior. 如果您打开文件的时间太长,请尝试使用AsyncWrapper,您将获得“即发即弃”的行为。

Therefore I think that the default value is ok. 因此我认为默认值是可以的。

I would try to send them an email and ask them about it. 我会尝试向他们发送电子邮件并询问他们。 I cannot figure out any reason why this option is false by default. 我无法弄清楚默认情况下此选项为false的任何原因。

The default value 'false' make no sense. 默认值'false'没有意义。 The expected behavior is fast and reasonable. 预期的行为是快速和合理的。

And keepFileOpen should never hold the file, because there is a FileStream flag called FileShare.ReadWrite . 而keepFileOpen永远不应该保存文件,因为有一个名为FileShare.ReadWriteFileStream标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM