当触发特定记录器时,如何获取以前记录的事件?

发布于 2024-08-30 03:34:17 字数 2922 浏览 5 评论 0原文

当触发特定记录器时,我需要显示前 10 个事件。目标是显示在发出 NHibernate.SQL 日志记录之前发生的先前步骤。

目前,我正在将 NHibernate sql 记录到一个单独的文件中 - 这工作正常。

<appender name="NHibernateSqlAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Logs\NHibernate.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10000KB" />
    <staticLogFileName value="true" />

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d{dd/MM/yy HH:mm:ss,fff} [%t] %-5p %c - %m%n"  />
    </layout>
</appender>

<logger name="NHibernate.SQL" additivity="false">
  <level value="ALL"/>
  <appender-ref ref="NHibernateSqlAppender"/>
</logger>
<logger name="NHibernate" additivity="false">
  <level value="WARN"/>
  <appender-ref ref="NHibernateSqlAppender"/>
</logger>

但这只输出 SQL,没有上下文。我希望也记录指定名称空间内的所有以前的日志,但当触发 HNibernate.SQL 附加程序时。

我研究了 BufferingForwardingAppender 作为一种手段的使用收集所有事件,然后在 NHibernateSqlAppender 中过滤它们,但这不起作用。我已经阅读了有关 LoggerMatchFilter 类的信息,这似乎会有所帮助,但我不确定将其放在哪里。

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

<appender name="NHibernateSqlAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Logs\NHibernate.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10000KB" />
    <staticLogFileName value="true" />

    <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="NHibernate.SQL" />
        <loggerToMatch value="Laan" />
    </filter>
    <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="NHibernate" />
        <acceptOnMatch value="false"/>
    </filter>

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d{dd/MM/yy HH:mm:ss,fff} [%t] %-5p %c - %m%n"  />
    </layout>
</appender>

<root>
    <level value="ALL" />
    <appender-ref ref="BufferingForwardingAppender"/>
</root>

这个想法是缓冲appender将存储所有事件,但是NHibernateSqlAppender只会在NHibernate.SQL事件触发时刷新,加上它将刷新缓冲区(在指定的记录器级别内的10个先前项目) ,在此示例中为 Laan。*)。

I need to show the previous 10 events when a particular logger is triggered. The goal is to show what previous steps occurred immediately before NHibernate.SQL logging was issued.

Currently, I am logging NHibernate sql to a separate file - this is working correctly.

<appender name="NHibernateSqlAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Logs\NHibernate.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10000KB" />
    <staticLogFileName value="true" />

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d{dd/MM/yy HH:mm:ss,fff} [%t] %-5p %c - %m%n"  />
    </layout>
</appender>

<logger name="NHibernate.SQL" additivity="false">
  <level value="ALL"/>
  <appender-ref ref="NHibernateSqlAppender"/>
</logger>
<logger name="NHibernate" additivity="false">
  <level value="WARN"/>
  <appender-ref ref="NHibernateSqlAppender"/>
</logger>

But this only outputs SQL, without context. I would like all previous logs within a specified namespace to also be logged, but only when the HNibernate.SQL appender is triggered.

I have investigated the use of BufferingForwardingAppender as a means to collect all events, and then filter them within the NHibernateSqlAppender, but this is not working. I have read about the LoggerMatchFilter class, which seems like it is going to help, but I'm not sure where to put it.

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

<appender name="NHibernateSqlAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Logs\NHibernate.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10000KB" />
    <staticLogFileName value="true" />

    <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="NHibernate.SQL" />
        <loggerToMatch value="Laan" />
    </filter>
    <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="NHibernate" />
        <acceptOnMatch value="false"/>
    </filter>

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d{dd/MM/yy HH:mm:ss,fff} [%t] %-5p %c - %m%n"  />
    </layout>
</appender>

<root>
    <level value="ALL" />
    <appender-ref ref="BufferingForwardingAppender"/>
</root>

The idea is that buffering appender will store all events, but then the NHibernateSqlAppender will only flush when an NHibernate.SQL event fires, plus it will flush the buffer (of 10 previous items, within the specified logger level, which in this example is Laan.*).

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

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

发布评论

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

评论(1

他夏了夏天 2024-09-06 03:34:17

我将实现 ITriggeringEventEvaluator,当某个记录器(理想情况下可配置)触发时,它会在 IsTriggeringEvent() 方法中返回 true。看看 LevelEvaluator 类的实现,应该很简单。代码如下所示:

public bool IsTriggeringEvent(LoggingEvent loggingEvent) 
{
    if (loggingEvent == null)
    {
        throw new ArgumentNullException("loggingEvent");
}

    return (loggingEvent.LoggerName == triggeringLoggerName); 
}

使用这个新的评估器,您的配置应该可以工作,尽管您应该删除过滤器(我猜)。

I would implement ITriggeringEventEvaluator that returns true in the IsTriggeringEvent() method when a certain logger (ideally configurable) fires. Have a look a the implementation of the LevelEvaluator class, it should be quite easy. The code would look like this:

public bool IsTriggeringEvent(LoggingEvent loggingEvent) 
{
    if (loggingEvent == null)
    {
        throw new ArgumentNullException("loggingEvent");
}

    return (loggingEvent.LoggerName == triggeringLoggerName); 
}

With this new evaluator your configuration should work though you should remove the filters (I guess).

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