NLog multiple processes
修复的问题汇总
Fix issue #804: Logging to same file from multiple processes misses messages
FileAppenders
MutexMultiProcessFileAppender.cs
RetryingMultiProcessFileAppender.cs
UnixMultiProcessFileAppender.cs
WindowsMultiProcessFileAppender.cs
Multi processes writing same file
The file target supports that multiple applications are concurrently writing to the same file. This mode is activated with concurrentWrites=true
. The IIS-application can run multiple AppDomains, and each AppDomain operates like a child process. When multiple processes are concurrently writing to the same file, then some locking for coordination is needed. NLog has support for the following locking-modes:
-
Operating System File Locks - Default mode that is supported on most platforms. This mode is enabled with
KeepFileOpen=False
. File locking from operating system are not fair and doesn't scale when having more than 2 processes writing to the same file. Exceptions will be thrown when one process tries to open a file, that is currently in use by another process. NLog tries to handle these exception by retrying on error (concurrentWriteAttempts=10
) together with randomizing how long to wait before retry (concurrentWriteAttemptDelay=1
). When you start to increase the number of processes, then the chance of failing 10 times in row increases, and so log messages are lost. -
Global Mutex Locks - NLog can use global mutex for inter-process communication, which is supported on many platforms. This mode is enabled with
KeepFileOpen=True
andConcurrentWrites=true
. The performance is better than file locks and with much less memory allocations. It works great on Windows, and most Linux platforms using NetCore2 or newer versions of MONO. But seldom很少 works for platforms for mobile devices like UWP / Xamarin Android / Xamarin iOS as these platforms runs in restricted sandbox without access to global mutex. -
Atomic File Append - .NET Framework on Windows has support for atomic file append mode. This mode is enabled with
KeepFileOpen=True
andConcurrentWrites=true
. This mode is only supported for .NET Framework on Windows platform. The lock synchronization is fair, and happens at the operating system level. It is the fastest concurrent mode available with minimum overhead.
It is recommended to enable asynchronous logging as it will reduce the overhead from file locking coordination.
Batch write and asynchronous logging
The file target has support for batch writing, where multiple log messages are written in one file-operation. Batch write will improve performance, especially when using KeepFileOpen=false
. Batch writing is enabled automatically when using the AsyncWrapper. This can be done by adding the attribute async="true"
to the <targets>
-element.
Asynchronous logging is recommended for multi-threaded server applications, but might not be worth it for quick-finishing command line application. Make sure to remember to flush at application shutdown, or else output can be dropped.
<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets async="true">
<target type="File" name="logfile" fileName="${basedir}/logs/${level}.txt" keepFileOpen="true" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-07-02 jQuery file upload callback options
2019-07-02 jQuery file upload process queue
2019-07-02 How do I add a simple onClick event handler to a canvas element?
2019-07-02 JavaScript 事件不触发
2019-07-02 jquery.fileupload-image-editor.js 中actions.resizeImage
2019-07-02 What's the difference between HEAD^ and HEAD~ in Git?
2017-07-02 Create the Project