log4net 何时将日志写入或提交到文件?
我们使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用的是
FileAppender
< /a>,此附加程序继承TextWriterAppender
,进而公开ImmediateFlush
属性。默认情况下,此属性的值为true
,并强制追加器对每个 Append 操作的基础流执行Flush()
。根据您设想客户“监视”日志文件的方式,一个想法可能是从您的应用程序内部启用监视。除了附加到文件之外,还可以使用 MemoryAppender 并从该附加器读取事件。
If you're using the
FileAppender
, this appender inherits theTextWriterAppender
, which in turn exposes theImmediateFlush
property. The value of this property istrue
by default, and forces the appender to do aFlush()
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.
当我在 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.
请参阅 这篇 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.
您谈论的是日志文件,因此大概您正在使用 FileAppender 或派生类。默认情况下这将缓冲输出。缓冲输出效率更高,因此为了满足您的要求,我建议您提供某种机制在查看日志之前刷新日志,而不是在每次写入操作后强制提交。
您可以使用如下代码来完成此操作:
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: