log4net 何时将日志写入或提交到文件?

发布于 2024-08-25 19:45:18 字数 139 浏览 6 评论 0原文

我们使用 log4net 来记录 winform 应用程序的事件和错误。 我们的客户希望在应用程序运行期间检查日志文件。 但我无法找出 log4net 何时以及如何执行写入(提交)操作。 以及如何满足客户的要求,除了我自己创建另一个记录器。 有什么帮助吗?谢谢。

We use the log4net to log the winform application's event and error.
Our customer want check the log file during the application running.
But I can't find out when and how the log4net do the write(commit) operation.
And how to meet the customer's requirement, except creating another logger by myself.
Any help? Thanks.

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

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

发布评论

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

评论(4

时光病人 2024-09-01 19:45:18

如果您使用的是 FileAppender< /a>,此附加程序继承 TextWriterAppender,进而公开 ImmediateFlush 属性。默认情况下,此属性的值为 true,并强制追加器对每个 Append 操作的基础流执行 Flush()

根据您设想客户“监视”日志文件的方式,一个想法可能是从您的应用程序内部启用监视。除了附加到文件之外,还可以使用 MemoryAppender 并从该附加器读取事件。

If you're using the FileAppender, this appender inherits the TextWriterAppender, which in turn exposes the ImmediateFlush property. The value of this property is true by default, and forces the appender to do a Flush() on the underlying stream for each Append operation.

Depending on how you envision the customer "monitoring" the log file, an idea could be to enable monitoring from within your application. This can be done by in addition to appending to a file, using the MemoryAppender and reading events from that appender.

绅刃 2024-09-01 19:45:18

当我在 SharePoint 实例中使用 log4net 时,我注意到的一件事是,根据您的安全配置和配置加载的实现,启动应用程序池的用户可能无权写入本地文件系统创建日志文件。

因此,您需要确保使用正确的凭据调用您的配置,在 SharePoint 中,它将位于提升的安全上下文中。不完全相关,但这可能是您遇到的问题。

One thing I did notice when I was using log4net in a SharePoint instance is that, depending on your security configuration and the implementation of your configuration loading, the user that spins up the application pool may not have rights to write to the local file system to create the log file.

Because of this you need to ensure that your configuration is invoked with the right credentials, in SharePoint it would be in an elevated security context. Not entirely related, but it might be an issue you run across.

又爬满兰若 2024-09-01 19:45:18

请参阅 这篇 SO 帖子,了解如何以编程方式刷新 log4net 日志。乔的答案虽然稍微不准确,但代码大致相同,因此可能适用于刷新所有缓冲的附加程序。

see this SO post for howto programmatically flush a log4net log. Joe's answer while slightly inaccurate is roughly the same code and so would presumably work for flushing all buffered appenders.

温柔少女心 2024-09-01 19:45:18

您谈论的是日志文件,因此大概您正在使用 FileAppender 或派生类。默认情况下这将缓冲输出。缓冲输出效率更高,因此为了满足您的要求,我建议您提供某种机制在查看日志之前刷新日志,而不是在每次写入操作后强制提交。

您可以使用如下代码来完成此操作:

foreach (IAppender appender in LogManager.GetRepository().GetAppenders())
{
    BufferingAppenderSkeleton b = appender as BufferingAppenderSkeleton;
    if (b != null) b.Flush();
}

You talk about a log file, so presumably you're using FileAppender or a derived class. This will buffer output by default. Buffered output is much more efficient, so to meet your requirement, I'd suggest you provide some mechanism to flush the log before viewing it, rather than forcing a commit after each write operation.

You can do this with code like the following:

foreach (IAppender appender in LogManager.GetRepository().GetAppenders())
{
    BufferingAppenderSkeleton b = appender as BufferingAppenderSkeleton;
    if (b != null) b.Flush();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文