log4net的有多个附加目的地可以写入同一个文件? [英] Can Log4net have multiple appenders write to the same file?

查看:594
本文介绍了log4net的有多个附加目的地可以写入同一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RollingFileAppender进行登录到一个conversionPattern文件的一些信息(在web.config)

I'm using a RollingFileAppender to log some info to a file with a conversionPattern (in the web.config) that looks like this for the header of each log section:

<conversionPattern value="%date - %property{userId} - %property{method}%newline--------------------------------%newline%message%newline%newline"/>

我想这个标题为要点下登录的细节。我目前正在使用另一个RollingFileAppender进行了记录到同一个文件只是一个破折号简单conversionPattern,像这样的:

I'd like to log details under this header as bullet points. I'm currently trying to use another RollingFileAppender that logs to the same file with a simple conversionPattern of just a dash, like this:

<conversionPattern value="- %message%newline"/>

但是,这些消息不会使它到日志文件。我使用Log.Info()页眉和Log.Debug()的要点和过滤各自的日志级别每个追加程序。就是我试图做可能吗?还是有更好的方法来从log4net的?

But these messages aren't making it into the log file. I'm using Log.Info() for the header and Log.Debug() for the bullet points and filtering each appender on their respective log levels. Is what I'm trying to do possible? Or is there a better way to get header and detail information into a log file from log4net?

推荐答案

是的,你可以有追加合并两个log4net的追加程序(写)到同一个日志文件。

Yes you can have two log4net appenders that append (write) to the same log file.

您需要将下面的行每个附加目的地的:

You need to place the following line in each of your Appenders:

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

这将使log4net的使用最小的锁定模式,允许多个进程写入同一文件。

This will make log4net use a minimal locking model that allows multiple processes to write to the same file.

下面是一个使用了两个附加目的地写同一个日志文件的示例XML:

Here's an example XML that uses two appenders writing to the same log file:

<log4net debug="false">
<appender name="RollingLogFileAppender1" type="log4net.Appender.RollingFileAppender">
  <!-- this configures a log for the application messages -->
  <file value="TestLog.log" />
  <appendToFile value="true" />
  <!-- next line uses a minimal locking model that allows multiple processes to write to the same file -->
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <!-- make the most recent log the highest numbered log -->
  <countDirection value="1" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5level %date{MM-dd-yyyy HH:mm:ss.ff} [%property{NDC}] %message%newline [Thread: %thread] %c{1} Method:%method(%file{1}, Line:%line) %newline" />
  </layout>
  <!-- The following two filters insure only log requests of 
        version '1' use this Appender -->
</appender>
<appender name="RollingLogFileAppender2" type="log4net.Appender.RollingFileAppender">
  <file value="TestLog.log" />
  <appendToFile value="true" />
  <!-- next line uses a minimal locking model that allows multiple processes to write to the same file -->
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <!-- make the most recent log the highest numbered log -->
  <countDirection value="1" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5level %date{MM-dd-yyyy HH:mm:ss.ff} [%property{NDC}] [Thread: %thread] %c{1} Method:%method(%file{1}, Line:%line) %newline%message" />
  </layout>
</appender>
<root>
  <level value="DEBUG" />
  <appender-ref ref="RollingLogFileAppender1" />
  <appender-ref ref="RollingLogFileAppender2" />
</root>

这可以在这里Apache的文档中找到:
的Apache log4net的文档
只要搜索这个页面相同的文件上。

This can be found in the Apache documentation here: Apache Log4Net Docs Just search on this page for 'same file'.

希望这有助于。

这篇关于log4net的有多个附加目的地可以写入同一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆