Log4Net 和并行实例的日志记录 [英] Log4Net and logging from parallel instances
问题描述
我在我的项目中使用 log4net,但有一个问题.该程序的主要功能需要很长时间,我使用日志记录来保存有关它的信息.我使用 FileAppender 将日志保存到文件中.
I'm using log4net in my project and there is one problem. The major function of the program takes a long time and I use logging to save information about it. I use FileAppender for saving log to file.
我的应用程序位于共享(本地)文件夹中,并且可能有多个应用程序实例从一个路径运行.在这种情况下,我只能从第一个程序记录信息,我的应用程序的其他实例无法记录信息因为日志文件被锁定.
My application is on the shared(local) folder and there could be several instances of the application running from one path. In this case I could log information only from the first program, other instances of my applications couldn't log info because log file is locked.
当我使用 "log4net.Appender.FileAppender+MinimalLock" 选项时,会出现信息丢失的情况.并非来自两个实例的所有日志都保存到文件中.
When I'm using "log4net.Appender.FileAppender+MinimalLock" option there are cases of information loss. Not all logs from both instances are saved to file.
如何解决这个问题并记录并行实例的信息?另外,当我使用MinimalLock"选项时性能下降怎么办?
How can I solve this problem and log info from parallel instances? Also what about performance degradation when I use "MinimalLock" option?
谢谢.希望得到您的帮助.
Thanks. Hope for your help.
推荐答案
只需在日志文件名中包含应用程序的进程 ID.然后,您的应用程序的不同实例将记录到不同的文件.下面是一个例子:
Simply include the process id of the application in the log file name. Different instances of your app will then log to different files. Here is an example:
<appender name="MyRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString">
<conversionPattern value="log_%processid.log" />
</file>
<!-- ... -->
这篇关于Log4Net 和并行实例的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!