How do I get multiple process to log to the same file?

来源:互联网 发布:人工智能软件下载 编辑:程序博客网 时间:2024/04/25 17:51

How do I get multiple process to log to the same file?

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. The FileAppender can be configured to use a different locking model, MinimalLock, that 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 loss in performance. See the FileAppender config examples for an example MinimalLock configuration.

While the MinimalLock model may be used to interleave writes to a single file it may not be the optimal solution, especially when logging from multiple machines. Alternatively you may have one or more processes log to RemotingAppenders. Using the RemoteLoggingServerPlugin (or IRemoteLoggingSink) a process can receive all the events and log them to a single log file.

 

http://logging.apache.org/log4net/release/faq.html

 

FileAppender

For full details see the SDK Reference entry: log4net.Appender.FileAppender.

The following example shows how to configure the FileAppender to write messages to a file. The file specified is log-file.txt. The file will be appended to rather than overwritten each time the logging process starts.

<appender name="FileAppender" type="log4net.Appender.FileAppender">    <file value="log-file.txt" />    <appendToFile value="true" />    <layout type="log4net.Layout.PatternLayout">        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />    </layout></appender>                

This example shows how to configure the file name to write to using an environment variable TMP. The encoding to use to write to the file is also specified.

<appender name="FileAppender" type="log4net.Appender.FileAppender">    <file value="${TMP}/log-file.txt" />    <appendToFile value="true" />    <encoding value="unicodeFFFE" />    <layout type="log4net.Layout.PatternLayout">        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />    </layout></appender>                

This example shows how to configure the appender to use the minimal locking model that allows multiple processes to write to the same file.

<appender name="FileAppender" type="log4net.Appender.FileAppender">    <file value="${TMP}/log-file.txt" />    <appendToFile value="true" />    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />    <layout type="log4net.Layout.PatternLayout">        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />    </layout></appender>                
http://logging.apache.org/log4net/release/config-examples.html#fileappender
原创粉丝点击