同一应用程序服务器的多个实例导致 2 个问题
Multiple instances of same application server cause 2 problems
我有一个 asp.net 应用程序的两个服务器实例。当我们转移到集群时,我发现我的一些日志(滚动)文件只有 2-3kb 而不是 15mb。当我的应用程序在一台服务器上时,它工作得很好。这是我对 log4net 的配置:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="\file_server\MyLog.xml"/>
<appendToFile value="true"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<datePattern value="ddMMyyyy"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="14"/>
<maximumFileSize value="15360KB"/>
<staticLogFileName value="true"/>
<countDirection value="1"/>
<layout type="log4net.Layout.XmlLayoutSchemaLog4j">
<locationInfo value="true"/>
</layout>
</appender>
我不确定这是错误还是其他原因。另外,我在这里发现了一些与此有关的问题,但没有解决方案:https://issues.apache.org/jira/browse/LOG4J2-174
你的问题是因为你正在从多个服务器写入同一个日志文件 ("value="\file_server\MyLog.xml")。这对一对夫妇来说是个坏消息观点。
- 当其中一个实例想要使用该文件时,该文件可能会被锁定,并且日志文件将不会更新。
- 您创建的每个日志都会产生网络流量,这是所有流量中最慢的。如果您正在编写 Async,那么您可能不会注意到这一点,但最好在本地编写日志,然后使用像 Splunk 这样的日志 reader 工具将它们关联起来。
评论太长了。
log4net documentation is very clear on this:
Before you even start trying any of the alternatives provided, ask yourself whether you really need to have multiple processes log to the same file, then don't do it ;-).
FileAppender offers pluggable locking models for this usecase but all existing implementations have issues and drawbacks.
By default the FileAppender holds an exclusive write lock on the log file while it is logging. This prevents other processes from writing to the file. This model is known to break down with (at least on some versions of) Mono on Linux and log files may get corrupted as soon as another process tries to access the log file.
MinimalLock only acquires the write lock while a log is being written. This allows multiple processes to interleave writes to the same file, albeit with a considerable loss in performance.
InterProcessLock doesn't lock the file at all but synchronizes using a system wide Mutex. This will only work if all processes cooperate (and use the same locking model). The acquisition and release of a Mutex for every log entry to be written will result in a loss of performance, but the Mutex is preferable to the use of MinimalLock.
If you use RollingFileAppender things become even worse as several process may try to start rolling the log file concurrently. RollingFileAppender completely ignores the locking model when rolling files, rolling files is simply not compatible with this scenario.
A better alternative is to have your processes log to RemotingAppenders. Using the RemoteLoggingServerPlugin (or IRemoteLoggingSink) a process can receive all the events and log them to a single log file. One of the examples shows how to use the RemoteLoggingServerPlugin.
或者您可以登录到数据库。
我有一个 asp.net 应用程序的两个服务器实例。当我们转移到集群时,我发现我的一些日志(滚动)文件只有 2-3kb 而不是 15mb。当我的应用程序在一台服务器上时,它工作得很好。这是我对 log4net 的配置:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="\file_server\MyLog.xml"/>
<appendToFile value="true"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<datePattern value="ddMMyyyy"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="14"/>
<maximumFileSize value="15360KB"/>
<staticLogFileName value="true"/>
<countDirection value="1"/>
<layout type="log4net.Layout.XmlLayoutSchemaLog4j">
<locationInfo value="true"/>
</layout>
</appender>
我不确定这是错误还是其他原因。另外,我在这里发现了一些与此有关的问题,但没有解决方案:https://issues.apache.org/jira/browse/LOG4J2-174
你的问题是因为你正在从多个服务器写入同一个日志文件 ("value="\file_server\MyLog.xml")。这对一对夫妇来说是个坏消息观点。
- 当其中一个实例想要使用该文件时,该文件可能会被锁定,并且日志文件将不会更新。
- 您创建的每个日志都会产生网络流量,这是所有流量中最慢的。如果您正在编写 Async,那么您可能不会注意到这一点,但最好在本地编写日志,然后使用像 Splunk 这样的日志 reader 工具将它们关联起来。
评论太长了。
log4net documentation is very clear on this:
Before you even start trying any of the alternatives provided, ask yourself whether you really need to have multiple processes log to the same file, then don't do it ;-).
FileAppender offers pluggable locking models for this usecase but all existing implementations have issues and drawbacks.
By default the FileAppender holds an exclusive write lock on the log file while it is logging. This prevents other processes from writing to the file. This model is known to break down with (at least on some versions of) Mono on Linux and log files may get corrupted as soon as another process tries to access the log file.
MinimalLock only acquires the write lock while a log is being written. This allows multiple processes to interleave writes to the same file, albeit with a considerable loss in performance.
InterProcessLock doesn't lock the file at all but synchronizes using a system wide Mutex. This will only work if all processes cooperate (and use the same locking model). The acquisition and release of a Mutex for every log entry to be written will result in a loss of performance, but the Mutex is preferable to the use of MinimalLock.
If you use RollingFileAppender things become even worse as several process may try to start rolling the log file concurrently. RollingFileAppender completely ignores the locking model when rolling files, rolling files is simply not compatible with this scenario.
A better alternative is to have your processes log to RemotingAppenders. Using the RemoteLoggingServerPlugin (or IRemoteLoggingSink) a process can receive all the events and log them to a single log file. One of the examples shows how to use the RemoteLoggingServerPlugin.
或者您可以登录到数据库。