强制 Log4Net RollingFileAppender 滚动

发布于 2024-07-11 20:18:12 字数 409 浏览 8 评论 0原文

根据Log4Net文档,RollingFileAppender只会记录消息时滚动日志文件。 我需要登录到这个文件,但每天都将其导入到另一个数据库中。 我无法使用数据库附加程序,因为我需要这些文件,并且必须将数据从日志文件转换到数据库(它不是直接副本)。 问题是,如果午夜之后没有日志活动,则日志不会滚动。 导入器查找前几天的文件(我无法更改此代码),因此如果没有活动并且日志未滚动,则导入器找不到该文件。 是否有办法强制日志在午夜滚动,而无需另一个线程唤醒并强制它滚动? 自定义附加器可以做到这一点吗? 如果可能的话我想避免这种情况。

According to the Log4Net documentation, the RollingFileAppender will only roll the log file when a message is logged. I need to log to this file, but import it every day into another database. I cannot use a database appender because I need the files and I have to translate the data from the log file to the database (it isn't a direct copy). The problem is if there is no log activity after midnight, the log doesn't roll. The importer looks for the previous days file (and I can't change this code), so if there is no activity and the log hasn't rolled, the importer doesn't find the file. Is there anyway to force the log to roll at midnight without having another thread that wakes up and forces it to roll? Could a custom appender do this? I would like to avoid this if possible.

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

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

发布评论

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

评论(3

失眠症患者 2024-07-18 20:18:12

编写一个 Windows 服务,在午夜过后触发一个事件,该事件使用相同的配置写入一个虚拟日志条目。

Write a Windows Service that fires an event just after midnight that writes a dummy log entry using the same configuration.

水波映月 2024-07-18 20:18:12

您必须从“什么代码路径导致翻转例程?”这个问题来考虑这一点。 一旦您知道如何实现该例程,您就可以决定如何触发它。

自定义附加器可以做到吗? 当然可以,但是在您通过附加程序登录之前,附加程序中的任何代码都不会运行,因此您回到了第一个方块。

至于问题“是否有办法强制日志在午夜滚动,而无需另一个线程唤醒并强制其滚动?”,我想说这个问题相当于“是否可以强制日志滚动”午夜没有运行任何代码?”。 我并不是想搞笑或侮辱你,我只是想以一种希望能为你解答的方式重述这个问题。 :-)

解决此问题的最简单方法是唤醒某些东西并记录以强制文件轮换。

You have to think about this from the point of the question "what code paths lead to the rollover routine?". Once you know how that routine is reached you can decide how to trigger it.

Could a custom appender do it? Sure, but no code in the appender will run until you log via it so you're back to square one.

As for the question "Is there anyway to force the log to roll at midnight without having another thread that wakes up and forces it to roll?", I would say that that question is equivalent to "Is it possible to force the log to roll at midnight without any code being run?". I'm not trying to be funny about it, or to insult you, I'm just trying to restate the question in a way which will hopefully answer it for you. :-)

The easiest way to solve this is to have something wake up and log to force the file to rotate.

已下线请稍等 2024-07-18 20:18:12

根据 RollingFileAppender 文档,您可以将其设置为滚动日常,请参阅此配置:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="logfile" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

According to the RollingFileAppender documentation you can set it to roll on a daily basis, see this configuration:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="logfile" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文