当 BufferingForwardingAppender 刷新时如何记录消息?

发布于 2024-11-06 05:15:04 字数 2640 浏览 2 评论 0原文

我有一个 BufferingForwardingAppender 配置将最后 10 条消息转发到 RollingFileAppender 当发生 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)

I have a BufferingForwardingAppender configured to forward the last 10 messages to a RollingFileAppender when an ERROR occurs. Can I get a custom message to appear in the log before each batch of messages?

I want to indicate that this is a new ERROR event and its context, so that the log is readable?

My log4net configuration is kinda like this:

<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>

And my desired output is kinda like this

---------- 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵强ㄟ 2024-11-13 05:15:04

我有一个可能适合您的解决方案,但它只是一种解决方法。基本上,设置两个附加程序写入同一个文件。当您写入错误(这将触发缓冲区清除到您的日志文件)时,让另一个附加程序首先写入“-----Error--------”文本,并在该错误之前添加换行符文本。

您将需要稍微尝试一下才能使其正常工作,但这应该是一个可行的解决方案。 Apache 确实允许多个附加程序写入同一个文件。只需确保将 +MinimalLock 放在附加程序上,这样它们就不会锁定文件。这是一个 SO 链接,解释了两个附加程序如何写入同一个文件:

Log4net 可以有多个附加程序写入同一个文件吗?

I've got a solution that will probably work for you but it is a bit of a work-around. Basically, set two appenders to write to the same file. When you write an error (which will trigger the Buffer to clear to your log file), have the other appender write the "-------Error--------" text first with a newline before the text.

You will have to play around with it a bit to get it to work, but this should be a workable solution. Apache does allow multiple appenders to write to the same file. Just make sure you put the +MinimalLock on your appenders so they don't lock the file. Here is a SO link that explains how two appenders can write to the same file:

Can Log4net have multiple appenders write to the same file?

-黛色若梦 2024-11-13 05:15:04

感谢 BiggsTRC伟大的答案。这是我的工作 log4net 配置和示例输出。

此附加程序通过在日志中写入上下文“标头”来响应任何ERROR消息。日志被最低限度地锁定,以允许该日志和缓冲附加程序写入文件。设置为 falseignoresException 标签实际上告诉 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>

此附加程序会缓冲所有消息,直到出现 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 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>

这些附加程序的输出如下所示

---------- 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()

Thanks to BiggsTRC for the great answers. This is my working log4net configuration and sample output.

This appender responds to any ERROR message by writing the context "header" in the log. The log is minimally locked to allow this and the buffering appender to write to the file. The ignoresException tag set to false actually tells log4net that this layout will write the exception (rather than it writing automatically below the message). But really I don't want it to, that's coming in the other appender.

<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>

This appender buffers all messages until an ERROR message appears, then it dumps the last 10 (bufferSize) messages and the error message to the 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>

This is the actual error file appender. The conversion pattern is standard. Notice the minimal file lock here as well.

<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>

The output of these appenders looks like this

---------- 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文