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.
我的应用程序位于shared(local)文件夹上,并且可能有多个应用程序实例从一条路径运行.在这种情况下,我只能记录第一个程序中的信息,而我的应用程序的其他实例不能记录信息,因为日志文件已锁定.
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屋!