When does log4net write or commit the log to file?

回眸只為那壹抹淺笑 提交于 2019-12-22 01:38:00

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/2452562/when-does-log4net-write-or-commit-the-log-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!