我有一个使用log4net的RollingFileAppender的客户端应用程序,可以多次实例化。最初我已将所有日志写入单个文件中,但是,我很快意识到log4net在写入时会锁定文件,但即使我使用less restrictive写入模式,我仍然会以我的日志文件中有很多乱七八糟的东西。
我决定将process-id合并到文件名中,如下所示:
<appender name="HumanRollingLog" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Log\TestLog[%processid].txt"/>
<param name="DatePattern" value="dd.MM.yyyy'.log'"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<staticLogFileName value="true" />
<maxSizeRollBackups value="10"/>
<maximumFileSize value="1KB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%type] [%thread] %-5level %logger - %message%newline%exception%"/>
</layout>
</appender>
那很有用。但是,它完全弄乱了滚动功能,因为现在每个进程都会生成自己的日志文件,实际滚动只会在进程ID开始重复之后发生。例如,启动我的应用程序3次,导致创建以下日志:
TestLog[5396].txt
TestLog[5396].txt.1
TestLog[5396].txt.10
TestLog[5396].txt.2
TestLog[5396].txt.3
TestLog[5396].txt.4
TestLog[5396].txt.5
TestLog[5396].txt.6
TestLog[5396].txt.7
TestLog[5396].txt.8
TestLog[5396].txt.9
TestLog[5976].txt
TestLog[5976].txt.1
TestLog[5976].txt.10
TestLog[5976].txt.2
TestLog[5976].txt.3
TestLog[5976].txt.4
TestLog[5976].txt.5
TestLog[5976].txt.6
TestLog[5976].txt.7
TestLog[5976].txt.8
TestLog[5976].txt.9
TestLog[6860].txt
TestLog[6860].txt.1
TestLog[6860].txt.10
TestLog[6860].txt.2
TestLog[6860].txt.3
TestLog[6860].txt.4
TestLog[6860].txt.5
TestLog[6860].txt.6
TestLog[6860].txt.7
TestLog[6860].txt.8
TestLog[6860].txt.9
任何人都知道如何解决此问题?我想让每个进程都有自己的文件,但我不能允许在所有进程中重用滚动。
谢谢!
答案 0 :(得分:1)
如果您坚持在日志文件的名称中使用进程标识符,那么内置滚动模式将永远无法正常工作。我想探讨你的要求。什么“我仍然会在我的日志文件中出现很多混乱”真的是什么意思?您试图从日志文件中得到什么答案?
针对其他问题的解决方案是将流程ID附加到日志消息,并使用众多可用工具之一(log4net dashboard,log4net viewer进行过滤/搜索, Apache Chainsaw,Microsoft LogParser或Kiwi LogViewer。