刷新BufferingForwardingAppender时如何记录消息?

时间:2011-05-09 21:24:42

标签: log4net log4net-configuration

我有一个BufferingForwardingAppender configured,可以在发生RollingFileAppender时将最后10封邮件转发给ERROR。在每批邮件之前,我是否可以在日志中显示自定义消息?

我想表明这是一个新的ERROR事件及其上下文,以便日志可读?

我的log4net配置有点像这样:

<appender name="ErrorBufferingAppender" type="log4net.Appender.BufferingForwardingAppender">
  <bufferSize value="10" />
  <lossy value="true" />
  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="ERROR" />
  </evaluator>
  <appender-ref ref="ErrorFileAppender" />
</appender>

<appender name="ErrorFileAppender" type="log4net.Appender.RollingFileAppender">
  <param name="File" value="logs\Errors.txt"/>
  <!-- other important parameters -->
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%-4thread] %-5level %logger{1} - %message%newline"/>
  </layout>
</appender>

我希望的输出有点像这样

---------- ERROR ----------
2011-05-09 16:59:12,327 [8   ] INFO  AppLogger - DiskStorageMonitor.StartMonitoring()
2011-05-09 16:59:12,331 [8   ] INFO  AppLogger - DiskStorageMonitor.Initializing File System Watcher...
2011-05-09 16:59:12,341 [8   ] INFO  AppLogger - DiskStorageMonitor - LocalSlideDataStorageDrive: C:
2011-05-09 16:59:12,370 [8   ] INFO  AppLogger - Transition: From 'MinimumDiskSpaceQuotaState' to 'IdleState'.
2011-05-09 16:59:26,697 [8   ] INFO  AppLogger - Transition: From 'IdleState' to 'SlideHolderMacroState'.
2011-05-09 16:59:26,702 [8   ] INFO  AppLogger - Transition: From 'SlideHolderMacroState' to 'WaitingForMacroCmdResponseState'.
2011-05-09 16:59:26,781 [12  ] INFO  YStageManager - Moving to: -1.25
2011-05-09 16:59:26,782 [14  ] INFO  XStageManager - Moving to: -142
2011-05-09 16:59:30,800 [12  ] ERROR RecoveringErrorHandler - An error occurred. Retrying... (snipped exception message & stacktrace)

---------- ERROR ----------
2011-05-09 16:59:30,808 [12  ] WARN  MacroCameraRecoverySteps - An error occurred while calling the macro camera. Resetting and retrying...
2011-05-09 16:59:30,809 [12  ] INFO  MacroCameraManager - Resetting the Macro Camera.
2011-05-09 16:59:30,886 [12  ] ERROR GetMacroImageCommandHandler - Macro image processing failed for the slide in position 0. (snipped exception message & stacktrace)

2 个答案:

答案 0 :(得分:2)

我有一个可能对你有用的解决方案,但这是一个解决方法。基本上,设置两个appender写入同一个文件。当你写一个错误(它会触发Buffer清除你的日志文件)时,让另一个appender先用换行符写“------- Error --------”文本。文本。

你必须稍微使用它才能使它工作,但这应该是一个可行的解决方案。 Apache允许多个appender写入同一个文件。只需确保将+ MinimalLock放在appender上,这样它们就不会锁定文件。这是一个SO链接,解释了两个appender如何写入同一个文件:

Can Log4net have multiple appenders write to the same file?

答案 1 :(得分:2)

感谢BiggsTRC获得了很好的答案。这是我工作的log4net配置和示例输出。

此appender通过在日志中写入上下文“header”来响应任何ERROR消息。最低限度地锁定日志以允许此缓冲appender写入文件。 ignoresException标记设置为false实际上告诉log4net 布局将编写异常(而不是在消息下面自动写入)。但实际上我并不想要它,而是另一个支持者。

<appender name="ErrorSectionAppender" type="log4net.Appender.FileAppender">
  <filter type="log4net.Filter.LevelMatchFilter">
    <levelToMatch value="ERROR" />
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
  <file value="logs\Errors.txt" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <ignoresException value="false" />
    <conversionPattern value="%newline---------- ERROR @ %date ----------%newline" />
  </layout>
</appender>

此appender缓冲所有消息,直到出现ERROR消息,然后将最后10条(bufferSize)消息和错误消息转储到ErrorFileAppender

<appender name="ErrorBufferingAppender" type="log4net.Appender.BufferingForwardingAppender">
  <bufferSize value="10" />
  <lossy value="true" />
  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="ERROR" />
  </evaluator>
  <appender-ref ref="ErrorFileAppender" />
</appender>

这是实际的错误文件appender。转换模式是标准的。请注意这里的最小文件锁定。

<appender name="ErrorFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="logs\Errors.txt"/>
  <appendToFile value="true"/>
  <staticLogFileName value="true"/>
  <rollingStyle value="Size"/>
  <maxFileSize value="1048576"/>
  <maxSizeRollBackups value="-1"/>
  <countDirection value="1"/>
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%-4thread] %-5level %logger{1} - %message%newline"/>
  </layout>
</appender>

这些appender的输出看起来像这样

---------- ERROR @ 2011-05-13 13:51:22,632 ----------
2011-05-13 13:51:04,937 [9   ] INFO  OmnyxScannerLogger - Transition: From 'No State' to 'CommunicationSyncState'.
2011-05-13 13:51:05,363 [12  ] DEBUG IMacroImageProcessor - hello, 10
2011-05-13 13:51:05,369 [19  ] INFO  XStageManager - Connecting
2011-05-13 13:51:05,369 [12  ] INFO  ZStageManager - Connecting
2011-05-13 13:51:05,369 [16  ] INFO  YStageManager - Connecting
2011-05-13 13:51:05,393 [9   ] INFO  OmnyxScannerLogger - Transition: From 'CommunicationSyncState' to 'SystemInitializeState'.
2011-05-13 13:51:08,528 [17  ] INFO  YStageManager - Homing
2011-05-13 13:51:08,539 [12  ] INFO  ZStageManager - Homing
2011-05-13 13:51:22,632 [14  ] ERROR RecoveringErrorHandler - An error occurred. Retrying...
Omnyx.Scanner.CliContracts.ScannerException: Injected: The stage failed to home.
   at Omnyx.Scanner.Simulation.ErrorInjection.ErrorInjectionInterceptor.Intercept(IInvocation invocation) in D:\Simulation\ErrorInjection\ErrorInjectionInterceptor.cs:line 42
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.IXStageManagerProxy.Home()

---------- ERROR @ 2011-05-13 13:51:33,758 ----------
2011-05-13 13:51:29,034 [9   ] INFO  OmnyxScannerLogger - DiskStorageMonitor.StartMonitoring()
2011-05-13 13:51:29,037 [9   ] INFO  OmnyxScannerLogger - DiskStorageMonitor.Initializing File System Watcher...
2011-05-13 13:51:29,046 [9   ] INFO  OmnyxScannerLogger - DiskStorageMonitor - LocalSlideDataStorageDrive: C:
2011-05-13 13:51:29,062 [9   ] INFO  OmnyxScannerLogger - -- LocalSlideDataStorageOverhead: 0
2011-05-13 13:51:29,075 [9   ] INFO  OmnyxScannerLogger - Transition: From 'MinimumDiskSpaceQuotaState' to 'IdleState'.
2011-05-13 13:51:31,727 [9   ] INFO  OmnyxScannerLogger - Transition: From 'IdleState' to 'SlideHolderMacroState'.
2011-05-13 13:51:31,732 [9   ] INFO  OmnyxScannerLogger - Transition: From 'SlideHolderMacroState' to 'WaitingForMacroCmdResponseState'.
2011-05-13 13:51:31,792 [12  ] INFO  YStageManager - Moving to: -1.25
2011-05-13 13:51:31,793 [20  ] INFO  XStageManager - Moving to: -142
2011-05-13 13:51:33,758 [12  ] ERROR RecoveringErrorHandler - An error occurred. Retrying...
Omnyx.Scanner.CliContracts.ScannerException: Injected: The macro camera failed.
   at Omnyx.Scanner.Simulation.ErrorInjection.ErrorInjectionInterceptor.Intercept(IInvocation invocation) in D:\Simulation\ErrorInjection\ErrorInjectionInterceptor.cs:line 42
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.IMacroCameraManagerProxy.TakeTopLitAndBackLitPictures()